Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 preg_match doesn't recognize \\2?
Index -> Programming, PHP -> PHP Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Amailer




PostPosted: Sat Jan 13, 2007 1:26 am   Post subject: preg_match doesn't recognize \\2?

Hey,
I have a place that does in one function

$text = preg_match("bla", "bla".valid_function("\\2")."bla");

In the valid function, it does another preg match, searching for a specific text; But, for some odd reason, its not taking the matched value for \\2, insted its taking \\2 as a string. So if I do a preg_match for "\\2", it works Sad

Any idea how to fix this?
Sponsor
Sponsor
Sponsor
sponsor
Tycoon




PostPosted: Sat Jan 13, 2007 2:06 am   Post subject: Re: preg_match doesn't recognize \\2?

I may have an idea, it may be simular to putting $array['item'] in a select statement - like so "SELECT * FROM `a` WHERE `id` = '$array['item']'";

I would try:

$valid_text = valid_function("\\2");

$text = preg_match("bla", "bla".$valid_text."bla");

Unsure if that will work but give it a try.
Amailer




PostPosted: Sat Jan 13, 2007 2:16 am   Post subject: RE:preg_match doesn\'t recognize \\2?

Nope, didn't work. I don't see how it would have made a difference though :/

Is there something I have to do in preg_match() to like..recognize \\2, $1, $2 and etc?
octopi




PostPosted: Sat Jan 13, 2007 2:26 am   Post subject: Re: preg_match doesn't recognize \\2?

I'm not exactly sure what your trying to do....but.....

preg_match returns an int, and not the acctual matched text. to access this, you need to add a 3rd parameter '

preg_match(/www\.(.*?).ca/,'www.compsci.ca',$matches);

now....$matches[0] should contain 'www.compsci.ca'
and $matches[1] should contain compsci

based on what your attempting to do (from what i can tell) you'll probally have to do a general match first, store it in matches, then feed that to your sub/function then do another match


I'm not exactly sure what your trying to do though....


\\2 from what I can tell your refering to a back reference, I don't believe you can place one of these into your haystack, as its meant to be part of your pattern.
Amailer




PostPosted: Sat Jan 13, 2007 3:21 am   Post subject: RE:preg_match doesn\'t recognize \\2?

K sorry, let me clear it up a bit.
This function searches for urls
php:
function make_clickable($text)
{
        $text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $text);

        // pad it with a space so we can match things at the start of the 1st line.
        $ret = ' ' . $text;

        // matches an "xxxx://yyyy" URL at the start of a line, or after a space.

        // xxxx can only be alpha characters.

        // yyyy is anything up to the first space, newline, comma, double quote or <

    $ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">".validate_url('$2')."</a>", $ret);

        // Remove our padding..

        $ret = substr($ret, 1);



        return($ret);
}



As you can see, it searches for the text http://blablabla in the post and makes it into a url. What I want to do is, if the urls domain matches that of the current site, strip the extra part, just show the page

So:
http://wii.com/index.php?t=12 -> index.php?t=12

That was suppose to have been done in the validate_url() function which has:

php:

        if( preg_match('#'.$forum_url.'#i', $url) ) {
                $valid = true;
        }


And some other stuff- but the preg_match there does not recognize the $2 given :/. Meaning, if I do

php:
        if( preg_match('#2#i', $url) ) {
                $valid = true;
        }


$valid will be true :/ Even if there is no 2 to match!
octopi




PostPosted: Sat Jan 13, 2007 4:50 am   Post subject: Re: preg_match doesn't recognize \\2?

You need to use preg_replace_callback() to do what you want, heres an example:
http://www.magnify.ca/devel/compsci/adm/preg.php
code:

<?php
  function validate_url($matches) {
    if($matches[2] == "www.magnify.ca") {
      return "<a href='{$matches[3]}'>{$matches[3]}</a> ";
    }
    else {
      return "<a href='{$matches[1]}{$matches[2]}{$matches[3]}'>{$matches[1]}{$matches[2]}{$matches[3]}</a> ";
    }
  }

  function make_clickable($subject)  {
   // $patterns[]="/http(s?):\/\/www.magnfiy.ca(:\d){1,5}\/(.*?)\w?/";  //our site
    $pattern="!(https?://)(.*?)(/.*?)*(\s|$)!";   //another site

    return preg_replace_callback($pattern,"validate_url",$subject);
  }

  print make_clickable("goto http://www.magnify.ca/index.php?test=hello&this=that now") . "<br>";
  print make_clickable("goto https://www.magnify.ca/index.php now") . "<br>";
  print make_clickable("goto https://www.compsci.ca/index.php now") . "<br>";
  print make_clickable("goto http://www.compsci.ca") . "<br>";
  print make_clickable("goto http://www.compsci.ca:8080") . "<br>";

?>


Amailer




PostPosted: Sat Jan 13, 2007 11:48 am   Post subject: RE:preg_match doesn\'t recognize \\2?

Excellent! That worked perfectly, thanks.

Though its, odd, I had seen what I was trying to do someone where else, and... it worked :/ Maybe I over looked something.
Tycoon




PostPosted: Sat Jan 13, 2007 1:49 pm   Post subject: Re: preg_match doesn't recognize \\2?

Lol I looked stupid Very Happy

Question though: How do you get the code to appear in its own box? Just use [code][/code] ?
Sponsor
Sponsor
Sponsor
sponsor
Amailer




PostPosted: Sat Jan 13, 2007 1:57 pm   Post subject: Re: preg_match doesn't recognize \\2?

Yeah, you can use [code][/code] or if you want to highlight it for a specific language use [syntax="langname"][/syntax]

A question: I got it to all work out, this is how it shows it now:
http://www.compsci.ca/v3/posting.php?t=123 Arrow posting.php?t=123

But I if I have just the domain, this happnes:
http://www.compsci.ca/v3/ Arrow [blank]

Is there a way to make the preg_replace() only remove the text if there is some text after the match?

***EDIT***
Nevermind, I got it Very Happy
Tycoon




PostPosted: Sun Jan 14, 2007 8:46 am   Post subject: Re: preg_match doesn't recognize \\2?

Amailer @ Sat Jan 13, 2007 1:57 pm wrote:
Nevermind, I got it Very Happy

Glad we could help Smile Don't be afraid to ask questions. haha
Tycoon




PostPosted: Tue Jan 16, 2007 1:27 pm   Post subject: Re: preg_match doesn't recognize \\2?

php:

<?php
                function replace_reply($message) {
                        $expression = "#\[reply=(.*?)](.*?)\[/reply]#si";
                       
                        $count = preg_match_all($expression, $message, $replace);
                       
                        if (count($count) > 0)
                        {
                                for ($a = 0; $a < count($count); $a++)
                                {
                                        $quote_user = $replace[1][$a];
                                        $replace_string = $replace[2][$a];
                                }
                                $str_replace = $this->str_replace_nonphp("Reply", $quote_user, $replace_string);
                                $message = preg_replace($expression, $str_replace, $message);
                        }
                       
                        return $message;
                }
?>


As we can see, this code SHOULD replace all the [reply] and [/reply] tags in the message BUT what it does is grab the first [reply=username] code and then it grabs, not the last [/reply] tag that matches, but the first one it fines.



aaaa.png
 Description:
Messed up function... tsk tsk tsk
 Filesize:  11.33 KB
 Viewed:  4346 Time(s)

aaaa.png


Display posts from previous:   
   Index -> Programming, PHP -> PHP Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: