SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    *********! *********!!! jackli's Avatar
    Join Date
    Sep 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_replace /e explanation and extension

    I know that the code below will send jack maple frost to the function search() ...

    $terms = "\"jack maple frost\"";
    preg_replace("/\"(.*?)\"/e", "search('\$1')", $terms);

    however, I'm wondering what's happening exactly. I know that when I change $1 to $0, the entire term, including quotes, "jack maple frost" is sent as argument into the function

    also, if
    $terms = "\"\"bad" quoting"
    the regular expression breaks apart

    how do you modify the regular expression so that it reinterprets an indefinite number of multiple quotes as just one quote -- and -- deletes the middle quote, so that there' just a beginning and ending quote?

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $terms '"Jack Maple Frost"';
        
    preg_replace('/^"(.*)?"$/e''search("$1")'$terms); 
    How it exactly works i don't know...i don't really dig down deep into things.

    Edit:


    e:

    If this modifier is set, preg_replace() does normal substitution of backreferences in the replacement string, evaluates it as PHP code, and uses the result for replacing the search string. Single and double quotes are escaped by backslashes in substituted backreferences.

    Only preg_replace() uses this modifier; it is ignored by other PCRE functions.
    Note: This modifier was not available in PHP 3.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i would do
    PHP Code:

    function callback($matches) {
        
    print_r($matches);// see what we got, all matches are in here

        
    return 'the replacement string';
    }

    preg_replace_callback('/"(.*)"/''callback'$terms); 
    you should use the 's' flag if you want the text between the quotes to be able to have a newline char in it. or just replace (.*) with ([^"]+)

  4. #4
    *********! *********!!! jackli's Avatar
    Join Date
    Sep 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think i mis-asked my question. this was what i wanted:

    if you have a search term with too many quotation marks and some quotations in the wrong place, is there a reg ex that can

    1. remove all excessive quotes, so that only the first and last quotes are shown -- 2 quotes total
    2. remove all quotes in middle between 2 end quotes

    here's an example:

    $term = ' ""apple berry" blitz"""" ';

    or

    $term = ' "jkl"""fkll "" kflk ii " ' ;

  5. #5
    *********! *********!!! jackli's Avatar
    Join Date
    Sep 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    basically, i'm accounting for malicious or clueless users who might enter in search terms into a search form with the wrong number of quotations.

  6. #6
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    str_replace() the quotes inside the callback function, using $matches[1] would work.

  7. #7
    *********! *********!!! jackli's Avatar
    Join Date
    Sep 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    not sure what you're saying... more details needed, please!

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function callback($matches) {
       
    // print_r($matches);// see what we got, all matches are in here

        
    return str_replace('"'''$matches[1]);


  9. #9
    *********! *********!!! jackli's Avatar
    Join Date
    Sep 2005
    Posts
    436
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, now is there even a need for the regular expression to process the term?

  10. #10
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    with the example $terms you gave, no. if these terms may be surrounded by other text, then yes.


Bookmarks

Posting Permissions

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