Computer Science Canada preg_match doesn't recognize \\2? |
Author: | Amailer [ 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 Any idea how to fix this? |
Author: | Tycoon [ 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. |
Author: | Amailer [ 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? |
Author: | octopi [ 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. |
Author: | Amailer [ 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
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:
And some other stuff- but the preg_match there does not recognize the $2 given :/. Meaning, if I do
$valid will be true :/ Even if there is no 2 to match! |
Author: | octopi [ 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
|
Author: | Amailer [ 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. |
Author: | Tycoon [ Sat Jan 13, 2007 1:49 pm ] |
Post subject: | Re: preg_match doesn't recognize \\2? |
Lol I looked stupid Question though: How do you get the code to appear in its own box? Just use [code][/code] ? |
Author: | Amailer [ 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 posting.php?t=123 But I if I have just the domain, this happnes: http://www.compsci.ca/v3/ [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 |
Author: | Tycoon [ 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
Glad we could help Don't be afraid to ask questions. haha |
Author: | Tycoon [ Tue Jan 16, 2007 1:27 pm ] | ||
Post subject: | Re: preg_match doesn't recognize \\2? | ||
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. |