string pop?<

liunx

Guest
I need something like array_pop but for a string. I need to pop something off the end of the string but I have no idea what the string length will be. the string will look something like so

$string = "text / text2 / text3 / text4 / text5 /";

so say I need to take "text5 /" off. for the life of me I just can't think straight. remember, text5 can be any given length.something like

$newstring=substr($string,0,strripos($string,'text5 /'));

or you could explode/array_pop/implodeA regex would do the job as well, presuming there are no forward slashes in the texts..


<?php

$string = "hey 1 / hey 2 / hey 3 / hey 4 / hey 5 /";

$string = preg_replace("/^(.*? )(?:[^\/]+\/)$/s", '\1', $string);

echo '>'.$string.'<';

?>


the forums won't leave the regex alone, the replacement is of course supposed to be '\1' and the regex itself "/^(.*? )(?:[^\/]+\/)$/s"illogique: thougth of that one, not sure if it works. I tried expode but it gave incomplete results. I would have to pop it twice seeing as it would contain a / in it. but even if you expoded it at the slash it creates undesirable effects.

Rydberg, thought about that as well but I cannot garauntee there will not be any slashes in the name.

as a side not, you can't use php tags for regex. you have to use code tags.Well.. If you're gonna have different texts in one string, you should better use a separator that you're sure won't show up..
It would be possible to have an array store the lengths of the strings you've added to the big string.. Not so pretty though.
Or just store the texts in an array and implode it when necessary, or loop through it.. my 2 cents.no I have to use a / as it symbolizes a directory structure. I need to find a way to pop the last added category that gets added if that directory is done and has no children.

see, this is part of a bigger script.
<!-- m --><a class="postlink" href="http://www.sitepoint.com/article/1105/2">http://www.sitepoint.com/article/1105/2</a><!-- m -->

I am using the script down towrds the bottom. the display_tree function.

it works fine, I had altered it to send me a option tag.
$list = "text / text2 / text3 / text4 /";
$row2['name'] = "text5 / ";

$right2[] = "<option value=\"subid1:{$row2['subid1']}\" selected>$list {$row2['name']}</option>\n";

but, when right[] gets popped it still keeps the original name in my array. I cannot pop my array because it would not pop the correct one.your string has a wrong format...
$list = "text / text2 / text3 / text4 /";
should be
$list = "text / text2 / text3 / text4 / ";
or
$list = "text / text2 / text3 / text4";
then you could explode on " / "

or loose all space like
$list = "text/text2/text3/text4/";it does have the space at the end, I just wrote it down wrong.

I think I have it. by using explode and implode I am able to make it work. funny I tried this the first time and it didn't work. seems that I had a slash in the wrong spot and it made everything mess up.

thanks illogique, just needed a fresh pair of eyes :)
 
Back
Top