Jump to content

[SOLVED] preg_match_all


phorcon3

Recommended Posts

i have the following url for example embedded in my site:

 

http://www.domain.com/page.php?first=1&second=2&third=3

 

how do i get each sub section?

 

that

 

first EQUALS 1

second EQUALS 2

etc.

 

so, i thought preg_match_all would be the way to go, ie:

 

$url= 'http://www.domain.com/page.php?first=1&second=2&third=3';

preg_match_all("first=\"(.*?)\"/", $url, $match);

$new = $match[1][0];

echo $new;

 

i must have messed something up, because it doesnt display anything ...i probably screwed up the pattern, or i dont know.. id really appreciate any help on this

 

thanks :D

Link to comment
https://forums.phpfreaks.com/topic/83189-solved-preg_match_all/
Share on other sites

I don't really see what your trying to do, but this might help...

 

<?php

    $s = 'http://www.domain.com/page.php?first=1&second=2&third=3';

    if (preg_match('/first=([0-9])&second=([0-9])&third=([0-9])/',$s,$a)) {
        print_r($a);
    } else {
        echo "no match found";
    }  
?>

Link to comment
https://forums.phpfreaks.com/topic/83189-solved-preg_match_all/#findComment-423180
Share on other sites

like for example i have:

 

<?php
$url = 'http://www.domain.com/page.php?id=8492038403&subid=8034';
?>

 

and i wanna extract the id 8492038403 or the subid 8034 ..but not at once, just one at a time

 

<?php

preg_match_all("id=\"(.*?)\"/", $url, $match);

echo 'id => '.$match[1][0];

preg_match_all("subid=\"(.*?)\"/", $url, $match);

echo 'subid => '.$match[1][0];

?>

 

if that makes any sense.. but the subid can be any character, its not only numeric chars

 

..but i just cant figure out how to do it

 

but thanks anyway;)

Link to comment
https://forums.phpfreaks.com/topic/83189-solved-preg_match_all/#findComment-423188
Share on other sites

This may be an easier method....

 

<?php

    $url = 'http://www.domain.com/page.php?first=1&second=2&third=3';

    $frags = parse_url($url,PHP_URL_QUERY);

    $elements = explode('&',$frags);

    foreach($elements as $element) {
        $parts = explode('=',$element);
        echo "{$parts[0]} = {$parts[1]}\n";
    }  

?>

Link to comment
https://forums.phpfreaks.com/topic/83189-solved-preg_match_all/#findComment-423195
Share on other sites

thanks for ya help! appreciate it;)

 

but i just figured out how to do it, here it is:

 

<?php
$rul = 'http://www.domain.com/page.php?id=8493k84903.84039&subid=8234908';

preg_match_all('/id=([a-zA-Z0-9\-\.]*)&subid=([a-zA-Z0-9\-\.]*)/', $url, $matches);


echo = 'id => '.$matches[1][0].'<br />subid => '.$matches[2][0];

?>

Link to comment
https://forums.phpfreaks.com/topic/83189-solved-preg_match_all/#findComment-423200
Share on other sites

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.