0

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!

3
  • $_GET['rid'] will do this for you ? Commented Jun 22, 2013 at 12:00
  • I think you need to pull that info from string, so for that you need preg_match Commented Jun 22, 2013 at 12:03
  • from what I understand, to use preg_match, I would need to know the characters I am looking for, which I don't. I only know what is before and after those characters. Commented Jun 22, 2013 at 12:09

3 Answers 3

1

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

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

to use the $_GET, that information needs to be displayed in the URL. The code I have is just a specific line from a .php file. Unless I am wrong or missing something...
1

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

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.