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

gwkg
gwkg used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
I think your best bet is to do as you suggested yourself... use explode() with <br /> as your delimiter then create a new string from the array. Should be very fast and low overhead. Don't forget to add back the final "</p>"

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

$arr = explode("<br />", $str );

$newstr = $arr[0] . "</br>" . $arr[1] . "</br>" . $arr[2] . "</br>" . $arr[3] . "</p>" ;

Commented:
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

Author

Commented:
@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.


Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Commented:
to get the 4th one, it can be repeated twice,
int lastIndxOfBr = strrpos ($string, "<br/>");
$string = substr ($string, 0, $lastIndxOfBr);
lastIndxOfBr = strrpos ($string, "<br/>");
$string = substr ($string, 0, $lastIndxOfBr) + "</p>";
 
//and the lines can be combined to avoid the step of 
//assigning the variable
 
$string = substr ($string, 0, strrpos ($string, "<br/>"));
$string = substr ($string, 0, strrpos ($string, "<br/>")) + "</p>;

Open in new window

Most Valuable Expert 2011
Top Expert 2016
Commented:
Not exactly sure of the format of your data.  For example, do you have anything that might look like this (note there are more lines in the address and the <BR> tag is slightly different, but equally valid):

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

Other things you might want to consider is what the data will look like with the phone number or web site omitted.  If you get a NULL placeholder for these, all is OK, but if you get something like this, it would not be so easy to get the parsing correct

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

This code snippet teaches an easy way to deal with some of the issues.  As far as speed goes, you do not even need to think about it.  Any difference in parsing techniques will be invisible in the cosmic scheme of things that make up a PHP page load - you're talking microseconds in a world of milliseconds.

best regards, ~Ray
<?php // RAY_temp_gwkg.php
error_reporting(E_ALL);
 
// TEST DATA, SLIGHTLY CHANGED FROM THE ORIGINAL POST
$str = "
<p>Local Buiness Name<br />
148 Main St<br />
Suite 105<br/>
Philadelphia, PA<br />
Center City<br />
(215)555-0123<br    />
companywebsite.com</p>";
 
// COPY AND MODIFY THE TEST DATA
$new = $str;
 
// STANDARDIZE THE VARIATIONS ON THE 'BREAK' TAG
$new = str_replace('<br', '<BR', $new);
$new = ereg_replace("<BR *", '<BR', $new);
$new = str_replace('<BR/>', '<br/>', $new);
 
// EXPLODE THE STRING INTO AN ARRAY
$arr = explode('<br/>', $new);
// var_dump($arr); // ACTIVATE THIS TO SEE THE ARRAY
 
// HOW MANY LINES OF ADDRESS ARE PRESENT? REMOVE THE LAST TWO - PHONE AND WEB SITE
$kount = count($arr)-1;
unset($arr[$kount], $arr[$kount-1]);
 
// RECONSTRUCT THE PARAGRAPH
$par = implode('<br/>', $arr) . '</p>';
 
// SHOW THE RESULTS
echo "<pre>";;
echo htmlentities($par);

Open in new window

Author

Commented:
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 =)
Most Valuable Expert 2011
Top Expert 2016

Commented:
Thanks for the points - it's a good questions. ~Ray

Commented:
Thanks for the points... :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial