Reputation: 391
I have the following string:
$out = '
<li style="margin: 0px; padding: 0px; ">myspace <a href="http://www.google.com/search?hl=en&lr=&q=myspace" rel="gb_pageset[]" title="Results @ Google" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/g-tiny.jpg" alt="Results @ Google" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a> <a href="http://search.yahoo.com/search?p=myspace" rel="gb_pageset[]" title="Results @ Yahoo" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/y-tiny.jpg" alt="Results @ Yahoo" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"> </a><a href="http://search.msn.com/results.aspx?FORM=MSNH&srch_type=0&q=myspace" rel="gb_pageset[]" title="Results @ MSN" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/m-tiny.jpg" alt="Results @ MSN" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a></li>
<li style="margin: 0px; padding: 0px; ">google <a href="http://www.google.com/search?hl=en&lr=&q=google" rel="gb_pageset[]" title="Results @ Google" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/g-tiny.jpg" alt="Results @ Google" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a> <a href="http://search.yahoo.com/search?p=google" rel="gb_pageset[]" title="Results @ Yahoo" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/y-tiny.jpg" alt="Results @ Yahoo" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"> </a><a href="http://search.msn.com/results.aspx?FORM=MSNH&srch_type=0&q=google" rel="gb_pageset[]" title="Results @ MSN" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/m-tiny.jpg" alt="Results @ MSN" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a></li>
<li style="margin: 0px; padding: 0px; ">youtube <a href="http://www.google.com/search?hl=en&lr=&q=youtube" rel="gb_pageset[]" title="Results @ Google" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/g-tiny.jpg" alt="Results @ Google" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a> <a href="http://search.yahoo.com/search?p=youtube" rel="gb_pageset[]" title="Results @ Yahoo" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/y-tiny.jpg" alt="Results @ Yahoo" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"> </a><a href="http://search.msn.com/results.aspx?FORM=MSNH&srch_type=0&q=youtube" rel="gb_pageset[]" title="Results @ MSN" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/m-tiny.jpg" alt="Results @ MSN" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a></li>
<li style="margin: 0px; padding: 0px; ">ebay <a href="http://www.google.com/search?hl=en&lr=&q=ebay" rel="gb_pageset[]" title="Results @ Google" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/g-tiny.jpg" alt="Results @ Google" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a> <a href="http://search.yahoo.com/search?p=ebay" rel="gb_pageset[]" title="Results @ Yahoo" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/y-tiny.jpg" alt="Results @ Yahoo" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"> </a><a href="http://search.msn.com/results.aspx?FORM=MSNH&srch_type=0&q=ebay" rel="gb_pageset[]" title="Results @ MSN" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/m-tiny.jpg" alt="Results @ MSN" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a></li>
<li style="margin: 0px; padding: 0px; ">yahoo <a href="http://www.google.com/search?hl=en&lr=&q=yahoo" rel="gb_pageset[]" title="Results @ Google" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/g-tiny.jpg" alt="Results @ Google" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a> <a href="http://search.yahoo.com/search?p=yahoo" rel="gb_pageset[]" title="Results @ Yahoo" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/y-tiny.jpg" alt="Results @ Yahoo" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"> </a><a href="http://search.msn.com/results.aspx?FORM=MSNH&srch_type=0&q=yahoo" rel="gb_pageset[]" title="Results @ MSN" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/m-tiny.jpg" alt="Results @ MSN" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a></li>
<li style="margin: 0px; padding: 0px; ">craigslist <a href="http://www.google.com/search?hl=en&lr=&q=craigslist" rel="gb_pageset[]" title="Results @ Google" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/g-tiny.jpg" alt="Results @ Google" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a> <a href="http://search.yahoo.com/search?p=craigslist" rel="gb_pageset[]" title="Results @ Yahoo" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/y-tiny.jpg" alt="Results @ Yahoo" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"> </a><a href="http://search.msn.com/results.aspx?FORM=MSNH&srch_type=0&q=craigslist" rel="gb_pageset[]" title="Results @ MSN" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/m-tiny.jpg" alt="Results @ MSN" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a></li>
<li style="margin: 0px; padding: 0px; ">you tube <a href="http://www.google.com/search?hl=en&lr=&q=you%20tube" rel="gb_pageset[]" title="Results @ Google" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/g-tiny.jpg" alt="Results @ Google" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a> <a href="http://search.yahoo.com/search?p=you%20tube" rel="gb_pageset[]" title="Results @ Yahoo" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/y-tiny.jpg" alt="Results @ Yahoo" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"> </a><a href="http://search.msn.com/results.aspx?FORM=MSNH&srch_type=0&q=you%20tube" rel="gb_pageset[]" title="Results @ MSN" style="margin: 0px; padding: 0px; text-decoration: none; color: rgb(51, 102, 255); "><img src="http://www.howrank.com/images/m-tiny.jpg" alt="Results @ MSN" border="0" style="margin: 0px; padding: 0px; " width="16" height="16"></a></li>
';
and basically want to echo everything that shows after
<li style="margin: 0px; padding: 0px; ">
and before
<a href="http://www.goo
The result I would like to see is:
myspace google ebay yahoo craigslist you tube
I tried various things that I found on stackoverflow but for some reason it either only returned one word or I got a 500 server error, so maybe YOU know the right solution.
For example:
$startsAt = strpos($out, '<li style="margin: 0px; padding: 0px; ">') + strlen('<li style="margin: 0px; padding: 0px; ">');
$endsAt = strpos($out, ' <a href="http://www.goo', $startsAt);
$result = substr($out, $startsAt, $endsAt - $startsAt);
echo $result;
I know, there is something missing like some foreach matching stuff, but since I am new to PHP, I am still stuck with this until I fully understand how this all works. I tried something like foreach($result as match) { echo $match; }
and so on, but with no success. Must be something that I am missing.
Don't be too harsh with me, I am still very new to PHP (started about a week ago) but I'm willing to learn :)
Thank you for your time.
Upvotes: 1
Views: 217
Reputation: 15892
There are a few things you could do here... explode
by line break (to get your <li>..</li>
lines as an array or us a regular expression - which granted have a bit of a learning curve. You're idea will work (almost there) but it relies on things being exactly formatted a certain way - there's a few ways to avoid that an have the same result.
<?php
$out=/*...*/
function findStart($string,$last=0) {
$start=strpos($string,"<li",$last);
if ($start===false) return -1;//No new start
$start=strpos($string,">",$start);
if ($start===false) return -1;//Mal formed <li>?
return $start+1;//Don't include the >
}
$start=0;
while (0<$start=findStart($out,$start)) {
$end=strpos($out," <",$start);
if ($end!==false) {
$set[]=substr($out,$start,$end-$start);
$start=$end;//Forward the pointer for the next loop
} else {$start=-1;}
}
//Now $set is an array of the values
print_r($set);
?>
Upvotes: 1
Reputation: 361947
if (preg_match_all('/>([^<>]+?) /', $out, $matches)) {
print_r($matches[1]);
}
This captures text between >
and
. The ([^<>]+?)
part matches any character except angle brackets [^<>]
repeated one or more times +
, finding the shortest matches possible ?
. The parentheses are used to capture these matching parts so we can access them via $matches[1]
afterwards.
Output:
Array
(
[0] => myspace
[1] => google
[2] => youtube
[3] => ebay
[4] => yahoo
[5] => craigslist
[6] => you tube
)
Upvotes: 2
Reputation: 4331
Are you looking to parse HTML with PHP, so you get everything (even possible nested HTML elements) in the LI? The problem is you can have the same LI code within another LI, so it can be tricky to do with string functions.
Perhaps DOM functions built into PHP can help here...
Upvotes: 0