
-----------------------------------
Amailer
Sat Jan 13, 2007 1:26 am

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 :(

Any idea how to fix this?

-----------------------------------
Tycoon
Sat Jan 13, 2007 2:06 am

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
Sat Jan 13, 2007 2:16 am

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
Sat Jan 13, 2007 2:26 am

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
Sat Jan 13, 2007 3:21 am

RE:preg_match doesn\'t recognize \\2?
-----------------------------------
K sorry, let me clear it up a bit. 
This function searches for urls
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("#(^|

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:


	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

	if( preg_match('#2#i', $url) ) {
		$valid = true;
	}

$valid will be true :/ Even if there is no 2 to match!

-----------------------------------
octopi
Sat Jan 13, 2007 4:50 am

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

