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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.