android6011 Posted July 24, 2009 Share Posted July 24, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/167298-grab-a-string-between-2-strings/ Share on other sites More sharing options...
mmarif4u Posted July 24, 2009 Share Posted July 24, 2009 If using space OR comma OR other, you can use explode function too. Like: Space $name = explode(" ",$name); echo $name[0]; OR Comma $name = explode(",",$name); echo $name[0]; Quote Link to comment https://forums.phpfreaks.com/topic/167298-grab-a-string-between-2-strings/#findComment-882108 Share on other sites More sharing options...
android6011 Posted July 24, 2009 Author Share Posted July 24, 2009 I have this working now, if anyone has a better way, I would appreciate it $namePre = explode('<td valign="top"><h2>',$content); $name = explode('</h2></td>',$namePre[1]); echo $name[0]; Quote Link to comment https://forums.phpfreaks.com/topic/167298-grab-a-string-between-2-strings/#findComment-882141 Share on other sites More sharing options...
The Eagle Posted July 24, 2009 Share Posted July 24, 2009 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."'"); Quote Link to comment https://forums.phpfreaks.com/topic/167298-grab-a-string-between-2-strings/#findComment-882148 Share on other sites More sharing options...
ldougherty Posted July 24, 2009 Share Posted July 24, 2009 You could use the strip_tags function.. http://us2.php.net/manual/en/function.strip-tags.php This should allow you to remove all of the HTML tags from your specified line leaving you with just the text. Quote Link to comment https://forums.phpfreaks.com/topic/167298-grab-a-string-between-2-strings/#findComment-882150 Share on other sites More sharing options...
vineld Posted July 24, 2009 Share Posted July 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/167298-grab-a-string-between-2-strings/#findComment-882153 Share on other sites More sharing options...
android6011 Posted July 24, 2009 Author Share Posted July 24, 2009 This one claims they have no XML feed Quote Link to comment https://forums.phpfreaks.com/topic/167298-grab-a-string-between-2-strings/#findComment-882160 Share on other sites More sharing options...
The Eagle Posted July 24, 2009 Share Posted July 24, 2009 Quote 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. Quote This one claims they have no XML feed You can make your own XML feed for your products. Quote Link to comment https://forums.phpfreaks.com/topic/167298-grab-a-string-between-2-strings/#findComment-882166 Share on other sites More sharing options...
bruce080 Posted July 24, 2009 Share Posted July 24, 2009 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++; } Quote Link to comment https://forums.phpfreaks.com/topic/167298-grab-a-string-between-2-strings/#findComment-882181 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.