By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,723 Members | 1,876 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,723 IT Pros & Developers. It's quick & easy.

PHP regex help

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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.