Forum Moderators: coopster

Message Too Old, No Replies

preg match question

preg match php

         

frogz

7:20 am on Jan 10, 2009 (gmt 0)

10+ Year Member



if (preg_match("/\google\b/i", $_SERVER['REQUEST_URI'])) {
print "yahooo";
} else {
print "hello";
}

cannot match anything within the query url?

so [domain.com...]

will return yahooo

why doesnt this work?

wildbest

2:03 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



the url is present and changes accordingly :)

What is changing? This URL should be [domain.com...] and should not change?!

frogz

2:05 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



does it matter this is a static url? although the dynamic url has "google" in it as well

frogz

2:07 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



lol yes its actually a query using a template. I am modifying the header, the "google.html" is one of the URLs in the script.

frogz

2:08 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



so when I am using the template when the word "google" comes across the url, I would like it to then print something extra

frogz

2:11 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



please also forgive me, I am totally using "google" hypothetically here, the *real* word in the query is "post" that I want flagged, so the url looks like index.php?post=44, then I made them static to post-44.html, I was using "google" as a reference, my apologies for not being clearer

frogz

2:13 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



(and yes i did change "google" to "post")

wildbest

2:16 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



So... Obviously a lot of mod_rewrite rewrites for friendly URLs :)

<?php
if (stripos ('google', $_SERVER['REQUEST_URI']) === true) {
echo "yahooo";
} elseif (stripos ('google', $_SERVER['QUERY_STRING']) === true) {
echo "google";
} else {
echo "hello microsoft";
}
?>

frogz

2:18 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



yes indeed! my htaccess scrollbar is very small!

thnx I will try the above recommendation!

=)

frogz

2:25 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



I'm sorry, I dont know why this is being so difficult, it still just prints the "else" argument.. regardless

"google" just slips by..

and "hello microsoft" remains through every URL

frogz

2:27 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



<?php
if (stripos ('post', $_SERVER['REQUEST_URI']) === true) {
echo "I wish I could see this";
} elseif (stripos ('post', $_SERVER['QUERY_STRING']) === true) {
echo "I wish I could see this";
} else {
echo "hello from Microsoft";
}
?>

frogz

2:30 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



I am using "post" as the word instead of "google" in the above case.
It looks like your code is pretty solid, and you are sure there is no error, perhaps I will play around with it, I see why it wont recognize the "post" though when it does in fact echo the query variable correctly.

thanks!

frogz

2:31 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



i *dont* see why*

wildbest

2:45 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



Must be something in the .htaccess rewrite rules or PHP header/script modifications.

yes its actually a query using a template. I am modifying the header, the "google.html" is one of the URLs in the script.

I'm still not sure what does that mean, but my understanding is that "google" should be present in the browser request not your internal server redirect/rewrite URL you setup in a script!

wildbest

2:50 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



Do you request that url by GET or POST method?

frogz

2:53 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



ok, i am using a template for the forum, it uses the same header information for each page. There is a "topic-id=" and there is a "post-id=" page within the forum script.

so if I use

<?php
if (stripos ('post', $_SERVER['REQUEST_URI']) === true) {
echo "I wish I could see this";
} elseif (stripos ('post', $_SERVER['QUERY_STRING']) === true) {
echo "I wish I could see this";
} else {
echo "hello from Microsoft";
}
?>

when the "post-id=" (which is now static "post-")page is requested, I would like it to print slightly different header info.

frogz

2:56 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



again, my apologies for the confusion, I am simply using "post-id-.html" instead of "google.htm"

i was using "google" just as a reference at first.

frogz

3:04 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



#request# GET [#*$!#*$!.com...]
GET /forums/post-44.html
#request# GET [#*$!#*$!.com...]
#request# GET [#*$!x.com...]

here is live header output

frogz

3:12 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



just intrigues me how it will echo the $_SERVER['REQUEST_URI' correctly, but not comply with the argument

eelixduppy

3:19 pm on Jan 10, 2009 (gmt 0)



You guys have the wrong understanding of what stripos is returning when it finds the string. It should be changed to this:

if (stripos ($_SERVER['REQUEST_URI'], 'post') [b]!== FALSE[/b]) {

update your code with that and try it.

[edited by: eelixduppy at 3:25 pm (utc) on Jan. 10, 2009]

frogz

3:19 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



maybe the random number such as: post-id-4444.htm
perhaps the numbers are throwing it off ?

frogz

3:27 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



eelix! the red ink pen comes out!

wildbest has been a saint! very helpful! (thank you wildbest)

now unfortunately.. it still didnt work :(

<?php
if (stripos ('post', $_SERVER['REQUEST_URI']) !== FALSE) {
echo "wanna see this";
} else {
echo "hello from microsoft";
}
?>
is my code,

thnx! regards, frogz

frogz

3:33 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



perhaps it has something to do with the template system, although it can clearly read the request variable, and "post" is definitely in the request.. I dunno, I use: $_SERVER['REQUEST_URI'] quite frequently actually.

frogz

3:42 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



I am about to fall out from exhaustion!
I will pick this back up, when I return!
wildbest thank you! cheers!
eelix thank you for your time as well, cheers!
will be back to check this thread later.

thanks again
frogz

eelixduppy

3:42 pm on Jan 10, 2009 (gmt 0)




('post', $_SERVER['REQUEST_URI'])

You have to switch these...


($_SERVER['REQUEST_URI'], 'post')

frogz

3:42 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



ok! will try that before i go =)

frogz

3:48 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



that did it! YESSS!

again, this site rocks, the people here are awesome!

thank you! eelix!

and thanks again wildbest!

:D

wildbest

4:19 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



You have to switch these...

LOL... Yes sure. Thanks, eelixduppy!

I'm still interested though why original preg_match I've suggested didn't work?

eelixduppy

6:05 pm on Jan 10, 2009 (gmt 0)



>> I'm still interested though why original preg_match I've suggested didn't work?

The one you suggested would work. We can't be sure how frogz changed the pattern for his own needs, switching it from 'google' to something else. Especially if he left an unescaped character in there it would have changed he pattern completely.

Glad everything is sorted now, though. :)

frogz

1:27 am on Jan 11, 2009 (gmt 0)

10+ Year Member



it is definitely within my experience there are several ways to accomplish something using php. I suspected wildbest was right on & accurate in his response. I cant be too sure why his original suggestion didnt work, perhaps underlying preexisting code, or like eelix mentioned, an escaped character. It is difficult and frustrating to help people sometimes when you cant actually see all of their code, or what exactly it is they are doing.

Again, thank you for your persistence in this matter wildbest, This provided me a solution to a problem I have been working with for a while now.

regards, frogz

This 59 message thread spans 2 pages: 59