Reputation: 23
Essentially, I have pulled the text from a URL and need to find a way to pull specific characters from the text.
The line I need pulled from is:
<p align="center"><a href="http://sitexplosion.com/?rid=1256" target="_blank">http://sitexplosion.com/?rid=1256</a></p>
The text I need pulled is essentially the number 1256, basically everything after ?rid= and before " target="_blank">
That number will change and will be anywhere from 1 to 6 characters in length.
If something like this has been posted already, I apologize. I have been scouring the net for the last 3 hours trying to find an answer of some sort.
If you can show me how to pull those characters from that line, I have got the rest already going.
Thanks in advance!
Upvotes: 0
Views: 70
Reputation: 768
Well this is much shorter
$string = strip_tags('<p align="center"><a href="http://sitexplosion.com/?rid=1256" target="_blank">http://sitexplosion.com/?rid=1256</a></p>');
echo str_replace('http://sitexplosion.com/?rid=','',$string);
Upvotes: 0
Reputation: 46620
Here, why not use a HTML parser or domdocument to extract the links, then get the links query params with parse_url()
$html = '
<p align="center"><a href="http://sitexplosion.com/?rid=1256" target="_blank">http://sitexplosion.com/?rid=1256</a></p>
<p align="center"><a href="http://sitexplosion.com/?rid=123456" target="_blank">http://sitexplosion.com/?rid=123456</a></p>
<p align="center"><a href="http://sitexplosion.com/" target="_blank">http://sitexplosion.com/</a></p>
';
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML($html);
$link_ids = array();
foreach ($dom->getElementsByTagName('a') as $link)
{
if($query = parse_url($link->getAttribute('href'), PHP_URL_QUERY))
{
$link_ids[] = str_replace('rid=','',$query);
}
}
print_r($link_ids);
/*
Array
(
[0] => 1256
[1] => 123456
)
*/
hope it helps
Upvotes: 1
Reputation: 4268
How about this one:-
$strout="<p align='center'><a href='http://sitexplosion.com/?rid=1256' target='_blank'>http://sitexplosion.com/?rid=1256</a></p>";
$startsAt = strpos($strout, "?rid") + strlen("?rid=");
$endsAt = strpos($strout, "{\'target}", $startsAt);
$result = substr($strout, $startsAt, ($endsAt-3) - $startsAt);
echo $result;
Output:-
Upvotes: 1