Link to home
Start Free TrialLog in
Avatar of gwkg
gwkgFlag for United States of America

asked on

Which PHP Str Function to Use For This....

A funciton in Wordpress is returning this

<p>Local Buiness Name<br />
148 Main Stt<br />
Philadelphia, PA<br />
Center City<br />
(215)555-0123<br />
companywebsite.com</p>

What is the appropriate function to use to cut out what is in bold

<br />
(215)555-0123<br />
companywebsite.com

And only leave

<p>Local Buiness Name<br />
148 Main St<br />
Philadelphia, PA<br />
Center City</p>

The actual data will be different each time so I can't use anything that counts the characters.

Should I use explode() at the <br /> tags and then rebuild the output from the pieces or is there a way to chop off everything at the 4th <br /> ?  

I'd like to choose based on execution speed, if possible.
SOLUTION
Avatar of CABIS
CABIS
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dhiraj05
dhiraj05

if you are using PHP later than 4, use strrpos - this will give the last index of string
int lastIndxOfBr = strrpos ($string, "<br/>");
$string = substr ($string, 0, $lastIndxOfBr) + "<p>";

Open in new window

Avatar of gwkg

ASKER

@dhiraj05: thanks, except that its not the last <br /> I need the pos of, its the 2nd to last <br /> .  Theres 5 <br /> tags, I want to lose the 4th one and everything after it.

@CABUS: thats pretty much what I have now, thanks for confirming its speed

$pieces = explode("<br />",$text);
for($i=0;$i<4;$i++)
        $excerpt .= $pieces[$i]."<br />";
return $excerpt;

i'll leave it open for the night and award the points in the morning.


SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gwkg

ASKER

Thanks everyone! I didn't know about var_dump (was using print_r) nor strrpos before this question, and will use unset in my new function =)
Thanks for the points - it's a good questions. ~Ray
Thanks for the points... :)