preg_match/replace to fix a search query

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fienen@gmail.com

    preg_match/replace to fix a search query

    I am working on a script to handle a search query. In some instances,
    the query could come through as "isbn:%20###### #######" (where %20 is
    an encoded space and the colon is optional). Basically I want to
    strip off the ISBN portion and leave just the numbers if that is the
    case.

    Orignally I was trying
    $value = $_GET["query"];
    if (preg_match("is bn:?%20", $value))
    {
    $isbn = preg_replace("i sbn:?%20", "", $value)
    }
    else
    {
    $isbn = $value
    }

    Doesn't seem to work though. Any thoughts?

  • fsidler@nospam.gmail

    #2
    Re: preg_match/replace to fix a search query

    On Mar 16, 5:19 pm, "fie...@gmail.c om" <fie...@gmail.c omwrote:
    I am working on a script to handle a search query. In some instances,
    the query could come through as "isbn:%20###### #######" (where %20 is
    an encoded space and the colon is optional). Basically I want to
    strip off the ISBN portion and leave just the numbers if that is the
    case.
    >
    Orignally I was trying
    $value = $_GET["query"];
    if (preg_match("is bn:?%20", $value))
    {
    $isbn = preg_replace("i sbn:?%20", "", $value)}
    >
    else
    {
    $isbn = $value
    >
    }
    >
    Doesn't seem to work though. Any thoughts?
    I wouldn't use regex unless really nessessary.
    I assume your problem lies with the space character. Usually, the
    script transforms encoded url characters back into their original
    form. I suggest using a normal space char in the script, and just to
    make sure, a str_replace("%2 0", " ", $string) in the beginning.

    Comment

    • Sjoerd

      #3
      Re: preg_match/replace to fix a search query

      fienen@gmail.co m wrote:
      if (preg_match("is bn:?%20", $value))
      ...
      Doesn't seem to work though. Any thoughts?
      - The pattern of preg_match needs delimiters. Mostly slashes are used
      for this: /pattern/
      - Each statement should be ended with a semicolon;
      - Note that PHP automatically URL-decodes the GET parameters, so %20 in
      the URL will become a space in the GET variable.
      - You can use the matches parameter to retrieve the ISBN number.
      - When asking a question here, "doesn't seem to work" is not good
      enough. Insert echo statements to see whether the preg_match actually
      matches and report any error you get in your post.

      if (preg_match("/isbn:? ([0-9X]*)/", $value, $match))
      {
      $isbn = $match[1];
      }
      else
      {
      $isbn = $value;
      }

      Sjoerd

      Comment

      • fienen@gmail.com

        #4
        Re: preg_match/replace to fix a search query

        Thanks for all the input. My solution ended up somewhat different,
        but in the same vein.

        Another script handed off the data based on a regex trigger, therefore
        I knew if this was invoked, it had to be a valid ISBN number, so no
        need to actually test if it matched since I know it does. If the
        query started with "ISBN" or "ISBN:", regardless of the number the
        total length would be 13, which is the longest the input could be
        and be a valid ISBN, so I used:

        if (strlen($bookIS BN) 13)
        {
        $bookISBN = preg_replace('/(\w+)(:)? (\d=)/', '$3', $bookISBN);
        }

        So far, so good. Everything works just as it should now.

        On Mar 16, 1:15 pm, Sjoerd <sjoerd-n...@linuxonly. nlwrote:
        fie...@gmail.co m wrote:
        if (preg_match("is bn:?%20", $value))
        ...
        Doesn't seem to work though. Any thoughts?
        >
        - The pattern of preg_match needs delimiters. Mostly slashes are used
        for this: /pattern/
        - Each statement should be ended with a semicolon;
        - Note that PHP automatically URL-decodes the GET parameters, so %20 in
        the URL will become a space in the GET variable.
        - You can use the matches parameter to retrieve the ISBN number.
        - When asking a question here, "doesn't seem to work" is not good
        enough. Insert echo statements to see whether the preg_match actually
        matches and report any error you get in your post.
        >
        if (preg_match("/isbn:? ([0-9X]*)/", $value, $match))
        {
        $isbn = $match[1];}
        >
        else
        {
        $isbn = $value;
        >
        }
        >
        Sjoerd

        Comment

        Working...