PHP regex help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • leegold2

    PHP regex help

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



    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';

    .....
  • Garp

    #2
    Re: PHP regex help


    "leegold2" <leegold2@veriz on.net> wrote in message
    news:wzMEc.2206 9$x9.10161@nwrd dc01.gnilink.ne t...[color=blue]
    > 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';
    >
    > ....[/color]

    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


    Comment

    • steve

      #3
      Re: Re: PHP regex help

      Garp wrote:[color=blue]
      > "leegold2" <leegold2@veriz on.net> wrote in message
      > news:wzMEc.2206 9$x9.10161@nwrd dc01.gnilink.ne t...[color=green]
      > > 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’;
      > >
      > > ....[/color]
      >
      > Without looking too closely or trying it, the // after http: are likely
      > being translated as pattern terminators. Escape them ("leaning[/color]
      toothpick"[color=blue]
      > syndrome ahoy), or change the terminators from / to something else[/color]
      like ?.[color=blue]
      > Escape anything that isn’t alphanumeric for safety, too (the :[/color]
      character).[color=blue]
      > $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[/color]
      or so a simple substring match:

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



      --
      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

      Comment

      • Garp

        #4
        Re: Re: PHP regex help


        "steve" <UseLinkToEmail @dbForumz.com> wrote in message
        news:22191c7e9c 8f84c52190f1f0b 04fc076@news.te ranews.com...[color=blue]
        > Garp wrote:[color=green]
        > > "leegold2" <leegold2@veriz on.net> wrote in message
        > > news:wzMEc.2206 9$x9.10161@nwrd dc01.gnilink.ne t...[color=darkred]
        > > > 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/",[/color][/color][/color]
        $data ) )[color=blue][color=green][color=darkred]
        > > > $data= 'You got a match';
        > > >
        > > > ....[/color]
        > >
        > > Without looking too closely or trying it, the // after http: are likely
        > > being translated as pattern terminators. Escape them ("leaning[/color]
        > toothpick"[color=green]
        > > syndrome ahoy), or change the terminators from / to something else[/color]
        > like ?.[color=green]
        > > Escape anything that isn't alphanumeric for safety, too (the :[/color]
        > character).[color=green]
        > > $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[/color]
        > or so a simple substring match:
        >
        > if (strstr($data, "http://library.dayton. town.net")) { ....
        >
        > http://ca3.php.net/manual/en/function.strstr.php
        >
        > --[/color]

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

        Garp


        Comment

        • bonehead

          #5
          Re: PHP regex help

          leegold2 wrote:[color=blue]
          > I'm new to PHP regex. What I am trying to match is:
          >
          > http://library.dayton.town.net[/color]

          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 = htmlspecialchar s("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

          Comment

          • leegold2

            #6
            Re: PHP regex help

            Thanks for all the help - I paid extra special
            attention to escaping the special chars and
            got it too work.

            Comment

            Working...