Hi there,
i've the strings, who containts this:
"asgaasg, gasgasg, saxx" or "asgaasg, saxxss". And I need always to get only the part of the strings for the last comma, so in th first example it is: "saxx" and in the second "saxxss". I tried to get that with using functions "strrpos" a "substr", but I wasn't success... Can you help me, please, how that problem to solve? E.g with using regular expression?

Thanks a lot!
Manny

Recommended Answers

All 9 Replies

Hi,

Check this out.

$text = "asgaasg, gasgasg, saxx";
echo $last = ltrim(strrchr($text, ", "), ", ");

I'm assuming the strings are variables. Here's a couple ways you could do it:

<?php
  $string1 = 'hello';
  $string2 = 'goodbye';

  echo $string1; //displays "hello"
  echo $string2; //displays "goodbye"
  echo $string1[3]; //displays "l"
  echo $string2[3]; //displays "d"

  /*The first character in a string is always 0 followed by 1, 2 etc until the end. In the above example, you can only grab one character from the string. If you wish to extract a smaller string of characters from the string, use the below example*/

  echo substr($string1, 1); //displays "ello"
  echo substr($string2, 1); //displays "oodbye"
  echo substr($string1, 1, 1); //displays "e"
  echo substr($string2, 1, 1); //displays "o"
  echo substr($string1, 1, 3); //displays "ell"
  echo substr($string2, 1, 3); //displays "ood"
  echo substr($string1, -3); //displays "llo"
  echo substr($string2, -3); //displays "bye"
  echo substr($string1, -3, 1); //displays "l"
  echo substr($string2, -3, 1); //displays "b"
  echo substr($string1, 1, -3); //displays "e"
  echo substr($string2, 1, -3); //displays "ood"
  echo substr($string1, -4, -3); //displays "he"
  echo substr($string2, -4, -3); //displays "od"

  /*The first number selects which character to start from and the second number says how many characters to display from the start. If the start is a negative value, it will count backwards from the end of the string. When doing this, the last character of the string is treated as -1 then the one before as -2 etc. If the length is negative then it will select the end character instead of the length of the string. If no length is specified then it will start at the start character and finish at the end of the string.*/
?>

Since you want it to start from the last comma, which is likely not always going to be in the same place each time, you would need some formula to count the amount of characters from the end to the last comma to know where to start from then use the reverse start method.

<?php
  $string1 = 'hello, goodbye, good day';
  $string2 = 'good day, goodbye, hello';

  for ($c = -1, $comma=0; $comma <= 0; $c--) //Start a loop to check for a comma in the string, starting from the end.
  {
    if (substr($string1, $c, 1) == ',') //Check the current character to see if it's a comma.
    {
      $comma = 1; //Set $comma to 1 so the for loop doesn't continue.
      $c = $c + 2; //Add 2 to $c to get rid of the comma and space before the last word in the string.
      $string1word = substr($string1, $c); //Set the last word of the string into a variable.
    };
  };

  for ($c = -1, $comma=0; $comma <= 0; $c--) //Start a loop to check for a comma in the string, starting from the end.
  {
    if (substr($string2, $c, 1) == ',') //Check the current character to see if it's a comma.
    {
      $comma = 1; //Set $comma to 1 so the for loop doesn't continue.
      $c = $c + 2; //Add 2 to $c to get rid of the comma and space before the last word in the string.
      $string2word = substr($string2, $c); //Set the last word of the string into a variable.
    };
  };

  echo $string1word; //Displays "good day"
  echo $string2word; //Displays "hello"
?>

Hope this helps.

Thanks guys for your replies!
I thougt about that problem and eventually I solved the problem with using the function explode():

$expl=explode(", ", $string]);	
	$count=count($expl);
	$new_size=trim($expl[$count-1]);

And it looks that it works

I was going to suggest array_pop(explode(', ', $string)); but the more I thought about it I figured creating an array out of n items in a string wasn't going to be the most efficient way to do it. So I ran some quick micro benchmarks to get an idea of just how much of a performance hit it would take.

I ran 4 different methods through the test and ran each test 5 times averaging the results.
array_pop - is the method I was going to suggest
trim - is the method Manny7 posted
ltrim - is the method paulrajj posted
substr - is the method Borzoi posted

I ran the benchmarks with strings made of 5 items, 100 items and 10000 items.

Results:
5 String Items
array_pop - .00003300s
trim - .00002360s
ltrim - .00001640s
substr - .00004559s

100 String Items
array_pop - .00010295s
trim - .00003781s
ltrim - .00001636s
substr - .000012765s

10000 String Items
array_pop - .00687265s
trim - .00320082s
ltrim - .00001755s
substr - .00005536s

The method posted my paulrajj is by far the fastest in every test. If you look at these quick benchmarks the ltrim method is almost 400x faster than array_pop when working with a substantial amount of items.

<?php
//paulrajj
$last = ltrim(strrchr($text, ", "), ", ");

**These were all run on a PHP 5.3.3 installation on Windows 7-64 Pro. Quad 2.8ghz - 6gb of memory. Your mileage may vary. You can see my benchmark script and my benchmark class in my repository on github: http://github.com/mikeschroeder/Benchmark/ Would be curious to see what other results were on different hardware/software configurations.

Glad my post is helpful. Thanks to mschroeder. :-)

Try this, nice and easy stuff ;)

$foo= split(",", $bar);// You got them in

//then get them like this

for($d=0:$d<count($foo);$d++){
echo $foo[$d]; // Print them out.

}

Explore :)

I added richieking's suggestion to my test code which is updated on github if interested.

For starters in php 5.3 split() is depreciated so its worth avoiding now.
preg_split is the suggested replacement, so I added this as well.

The results were pretty much what I expected this was a snapshot of a run with 10000 items in the string:

array_pop test
Total Time: 0.0065879822 seconds.
---------------------------------------
explode test
Total Time: 0.0072600842 seconds.
---------------------------------------
trim test
Total Time: 0.0030097961 seconds.
---------------------------------------
ltrim test
Total Time: 0.0000169277 seconds.
---------------------------------------
substr test
Total Time: 0.0000491142 seconds.
---------------------------------------
split test
Total Time: 0.3050770760 seconds.
---------------------------------------
preg_split test
Total Time: 0.0080001354 seconds.

Not bad,

Try strstr($haystack, $needle) function and print the benchmas.

strstr() does not satisfy the ops original conditions. The op wants the last item in a comma delimited string, without knowing what the item is prior.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.