Results 1 to 5 of 5

PHP Question

This is a discussion on PHP Question within the Off-Topic & Chit Chat forums, part of the Focus on Members category; I know this is the wrong type of forum, but know there are members here that code. Just a quick ...

  1. #1
    Senior Member woostar's Avatar
    Real Name
    woostar
    Join Date
    Apr 2007
    Posts
    788
    Liked
    5 times

    PHP Question

    I know this is the wrong type of forum, but know there are members here that code.

    Just a quick question:

    I'm trying to replace or remove wildcards in keywords but think I'm over complicating it (but being crap at pattern matching so a bit stumped).


    I want to remove first and last %, and if there is no % add a space.
    Example replacements:
    '%keyword%' => 'keyword'
    'keyword%' => ' keyword'
    '%keyword' => 'keyword '
    'keyword' => ' keyword '


    This is the way I've done it, but is there a better way of doing it:
    Code:
    $keyword = $keyword[0] != '%' ? ' ' . $keyword : substr($keyword, 1);
    $keyword = $keyword[strlen($keyword)-1] != '%' ? $keyword . ' ' : substr_replace($keyword, '', -1);
    If anyone can think of a faster way of doing it I would be grateful
    2011 Average: 1 post every 5 seconds... 24/7

  2. #2
    vBSEO Staff Brian Cummiskey's Avatar
    Real Name
    Brian Cummiskey
    Join Date
    Jul 2009
    Location
    btwn NYC and Boston
    Posts
    12,789
    Liked
    657 times
    Blog Entries
    2
    preg_replace might be the better function to use.

    I don't have a code sample at the moment....
    PHP: preg_replace - Manual

  3. #3
    vBSEO Staff Oleg Ignatiuk's Avatar
    Real Name
    Oleg Ignatiuk
    Join Date
    Jun 2005
    Location
    Belarus
    Posts
    25,744
    Liked
    169 times
    Something like this should work:
    $keyword = preg_replace('#^\%?(.*?)\%?$#', '$1', $keyword);

  4. #4
    Senior Member woostar's Avatar
    Real Name
    woostar
    Join Date
    Apr 2007
    Posts
    788
    Liked
    5 times
    Thank you Oleg and Brian.

    Gave it a go. It works great for removing % but doesn't add a space if there is no %.

    I've changed my version to use trim() instead of substr.
    Code:
    $keyword = $keyword[0] != '%' ? ' ' . $keyword : ltrim($keyword, '%');
    $keyword = $keyword[strlen($keyword)-1] != '%' ? $keyword . ' ' : rtrim($keyword, '%');
    Performance wise, does the difference in methods matter that much?
    2011 Average: 1 post every 5 seconds... 24/7

  5. #5
    vBSEO Staff Oleg Ignatiuk's Avatar
    Real Name
    Oleg Ignatiuk
    Join Date
    Jun 2005
    Location
    Belarus
    Posts
    25,744
    Liked
    169 times
    That regular expression is quite simple, so performance wise there won't be a noticeable difference.

Similar Threads

  1. Question
    By The3rdNipple in forum Pre-Sales Questions
    Replies: 10
    Last Post: 12-05-2008, 02:31 PM
  2. Two question
    By Bridges in forum Troubleshooting
    Replies: 1
    Last Post: 12-16-2005, 10:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •