467,104 Members | 1,024 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,104 developers. It's quick & easy.

PHP regex help

I'm new to PHP regex. What I am trying to match is:

http://library.dayton.town.net

it's a mysql/php field.

Then if a match I take some action.

But it's not working any help appreciated. Thanks
while ( $row = mysql_fetch_row( $result ) ) {...
.....
foreach($row as $data) {
if ( preg_match ( "/http://library\.dayton\.town\.net/", $data ) )
$data= 'You got a match';

.....
Jul 17 '05 #1
  • viewed: 1523
Share:
5 Replies

"leegold2" <le******@verizon.net> wrote in message
news:wz******************@nwrddc01.gnilink.net...
I'm new to PHP regex. What I am trying to match is:

http://library.dayton.town.net

it's a mysql/php field.

Then if a match I take some action.

But it's not working any help appreciated. Thanks
while ( $row = mysql_fetch_row( $result ) ) {...
....
foreach($row as $data) {
if ( preg_match ( "/http://library\.dayton\.town\.net/", $data ) )
$data= 'You got a match';

....


Without looking too closely or trying it, the // after http: are likely
being translated as pattern terminators. Escape them ("leaning toothpick"
syndrome ahoy), or change the terminators from / to something else like ?.
Escape anything that isn't alphanumeric for safety, too (the : character).
$pattern="?http\://library\.dayton\.town\.net?";
or
$pattern="/http\:\/\/library\.dayton\.town\.net/";
Then
if ( preg_match ( $pattern, $data ) )
Etc.

Of course, in this case you could always use
if($data == "http://library.dayton.town.net")
........

lol

Garp
Jul 17 '05 #2
Garp wrote:
"leegold2" <le******@verizon.net> wrote in message
news:wz******************@nwrddc01.gnilink.net...
Iím new to PHP regex. What I am trying to match is:

http://library.dayton.town.net

itís a mysql/php field.

Then if a match I take some action.

But itís not working any help appreciated. Thanks
while ( $row = mysql_fetch_row( $result ) ) {...
....
foreach($row as $data) {
if ( preg_match ( "/http://library\.dayton\.town\.net/", $data ) )
$data= íYou got a matchí;

....
Without looking too closely or trying it, the // after http: are likely
being translated as pattern terminators. Escape them ("leaning

toothpick" syndrome ahoy), or change the terminators from / to something else like ?. Escape anything that isnít alphanumeric for safety, too (the : character). $pattern="?http\://library\.dayton\.town\.net?";
or
$pattern="/http\:\/\/library\.dayton\.town\.net/";
Then
if ( preg_match ( $pattern, $data ) )
Etc.

Of course, in this case you could always use
if($data == "http://library.dayton.town.net")
........

lol

Garp

or so a simple substring match:

if (strstr($data, "http://library.dayton.town.net")) { ....

http://ca3.php.net/manual/en/function.strstr.php

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-regex-he...ict125133.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=416874
Jul 17 '05 #3

"steve" <Us************@dbForumz.com> wrote in message
news:22******************************@news.teranew s.com...
Garp wrote:
> "leegold2" <le******@verizon.net> wrote in message
> news:wz******************@nwrddc01.gnilink.net...
> > I'm new to PHP regex. What I am trying to match is:
> >
> > http://library.dayton.town.net
> >
> > it's a mysql/php field.
> >
> > Then if a match I take some action.
> >
> > But it's not working any help appreciated. Thanks
> >
> >
> > while ( $row = mysql_fetch_row( $result ) ) {...
> > ....
> > foreach($row as $data) {
> > if ( preg_match ( "/http://library\.dayton\.town\.net/", $data ) ) > > $data= 'You got a match';
> >
> > ....

>
> Without looking too closely or trying it, the // after http: are likely
> being translated as pattern terminators. Escape them ("leaning

toothpick"
> syndrome ahoy), or change the terminators from / to something else

like ?.
> Escape anything that isn't alphanumeric for safety, too (the :

character).
> $pattern="?http\://library\.dayton\.town\.net?";
> or
> $pattern="/http\:\/\/library\.dayton\.town\.net/";
> Then
> if ( preg_match ( $pattern, $data ) )
> Etc.
>
> Of course, in this case you could always use
> if($data == "http://library.dayton.town.net")
> ........
>
> lol
>
> Garp

or so a simple substring match:

if (strstr($data, "http://library.dayton.town.net")) { ....

http://ca3.php.net/manual/en/function.strstr.php

--


This is turning into a JAPH thread. 8D (I tried my techniques for the OP
today, they're both good).

Garp
Jul 17 '05 #4
leegold2 wrote:
I'm new to PHP regex. What I am trying to match is:

http://library.dayton.town.net


Dude,

This isn't exactly your script, but paste this into a new .php file,
upload it to your server, run it in a browser, and tell me if it echos
"You got a match":

/*BEGIN SCRIPT*/
<?

$data = htmlspecialchars("http://library.dayton.town.net");
if ( preg_match ( '[http://library\.dayton\.town\.net]', $data ) ) {
echo "You got a match";
} else {
echo "You did not get a match";
}
?>
/*END SCRIPT*/

--
'bonehead

Jul 17 '05 #5
Thanks for all the help - I paid extra special
attention to escaping the special chars and
got it too work.
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by jeevankodali@gmail.com | last post: by
17 posts views Thread by clintonG | last post: by
7 posts views Thread by Mike Labosh | last post: by
9 posts views Thread by jmchadha@gmail.com | last post: by
7 posts views Thread by Extremest | last post: by
6 posts views Thread by Phil Barber | last post: by
reply views Thread by Support Desk | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.