Jump to content

grab a string between 2 strings


android6011

Recommended Posts

I am making a store website, and to add products from the wholeseller they are allowing screen grabs. The Info is displayed, for example like:

<td valign="top"><h2>Name of Product</h2></td>

All I want is the name. A lot of people have said to use preg_match_all, btu I can't get it to work.

 

Can someone tell me another way or show me an example of preg_match_all getting just the name from the above? Thanks!

 

Link to comment
https://forums.phpfreaks.com/topic/167298-grab-a-string-between-2-strings/
Share on other sites

Well on my PHP billing system to display the package name I use,

 

 <?php echo $arrPlan["itemname"];?>

 

Which $arrPlan is then identified above as,

 

$arrPlan = mysql_fetch_array($rsPlan);

 

Finally,  $rsPlan is identified as,

 

$rsPlan = mysql_query("select * from package where planname='".$PlanExist."'");

If the strings look like that all the time you could simply use strip_tags as suggested although I doubt it. preg_match_all and regular expression patterns is what you will have to use otherwise.

 

You should be able to receive an xml feed of the products though if you ask your wholeseller which will make your life easier.

If the strings look like that all the time you could simply use strip_tags as suggested although I doubt it. preg_match_all and regular expression patterns is what you will have to use otherwise.

 

You should be able to receive an xml feed of the products though if you ask your wholeseller which will make your life easier.

 

It's easy to echo MySQL queries that you're grabbing. Look at my example post a few above yours.

 

This one claims they have no XML feed

 

 

You can make your own XML feed for your products.

Well, you could try:

$html = <td valign=\"top\"><h2>Apples</h2></td>
$regex = "/(<([\w]+)[^>]*>)*(.*?)(<\/\\2>)/";
preg_match_all($regex, $html, $matches, PREG_SET_ORDER);

$i=0;
foreach ($matches as $val) {
$name[$i] = $val[3];
$i++;
}

 

Where the name array would be an array of all the products and you just have to specify which lines of the program have the product lines listed

 

or the following if the tags <td valign=\"top\"><h2> will always be there:

$html = <td valign=\"top\"><h2>Apples</h2></td>
$regex = "/(^<td valign=\"top\"><h2>)(.*?)(<\/h2><\/td>)/";
preg_match_all($regex, $html, $matches, PREG_SET_ORDER);
$i=0;
foreach ($matches as $val) {
$name[$i] = $val[2];
$i++;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.