Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Exclamation preg_replace advice please

    Hi All,

    i am relitavly new to preg_replace and i have several statments which pulls out specific text from a string, what i am wondering is, if it is possible to use just one expression to do what these lines do.

    here is my code
    PHP Code:
                $shortTitle htmlspecialchars($row['short_title']);
                
    $shortTitle preg_replace("/.{4}(-inch).*/"" "$shortTitle) . "\n";
                
    $shortTitle preg_replace("/.{4}( inch).*/"" "$shortTitle) . "\n";
                
    $shortTitle preg_replace("/.{0}(\/).*/"""$shortTitle) . "\n";
                
    $shortTitle preg_replace("/.{0}(Laptop).*/"" "$shortTitle) . "\n";
                
    $shortTitle preg_replace("/.{4}(GHz).*/"" "$shortTitle) . "\n";
                
    $shortTitle preg_replace("/.{4}(Ghz).*/"" "$shortTitle) . "\n";
                
    $shortTitle preg_replace("/.{4}(-Inch).*/"" "$shortTitle) . "\n";
                
    $shortTitle preg_replace("/.{0}(Notebook).*/"" "$shortTitle) . "\n";
                
    $shortTitle trim($shortTitle);
                
    $shortTitle trim($shortTitle,'\,'); 
    just makes it look tidier and will hopefully help me to learn preg_replace a bit more

    p.s if you could comment each section to say what it does that will be a big help.

    many thanks
    Luke

  • #2
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    yes it is and elaberate more on the functuality of these regexps
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    ok i am pulling info from feeds and the info im pulling is the product titles

    for example
    Sony VAIO NS30E/S Laptop - Intel Pentium T4200 3 Gb 250 Gb DVD+-RW/+-R DL/RAM 15.4 inch WXGA Vista Home premium

    or
    HP Pavilion dv7-2215SA 17.3-inch Notebook

    the titles vary in structure my reg exp statments cater for each of the different title structure so that i am able to just have the model name and number

    e.g with the above examples
    Sony VAIO NS30E or HP Pavilion dv7-2215SA

    so each of those reg exps removes a bit of the titles i dont want.

    and there for each of my 20 plus items now displays a short title which looks cleaner and tidier but very long winded in my opinion

    cheers
    Luke

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    for example this line

    using this title:
    Sony VAIO NS30E/S Laptop - Intel Pentium T4200 3 Gb 250 Gb DVD+-RW/+-R DL/RAM 15.4 inch WXGA Vista Home

    this line:
    $shortTitle = preg_replace("/.{5}(inch).*/", " ", $shortTitle) . "\n";

    would search for the word inch and remove everything after it and everything 5 places before it so i would be left with this title

    "Sony VAIO NS30E/S Laptop - Intel Pentium T4200 3 Gb 250 Gb DVD+-RW/+-R DL/RAM "

    obviously for this example
    $shortTitle = preg_replace("/.{1}(Laptop).*/", " ", $shortTitle) . "\n"; would be best because it would leave me with a title

    Sony VAIO NS30E/S

    but because there are so many possibilitys in the titles i would not beable to use the above because it may mess up obe of the other titles if you see what i mean

    cheers

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    Hi All.

    New help needed with a regular expression, didnt want to tart a new thread because it relates to this topic

    i have this expression
    PHP Code:
    $prodShortName preg_replace("/[^a-zA-Z0-9&;\s]|[\s-]/"""$prodShortName); 
    which removes everything not letters numbers & or ;
    the trouble im having is with the second part of the expression [\s-] what i am trying to achieve is for it not to remove all -'s but only if there is a space infront of it like so " -".

    but the current expression is removing all - and all spaces?

    anyone got any ideas how i can remove just the - which have a space infront of them?

    thanks
    Luke

  • #6
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    EDIT: thats wrong... 2 mins..
    Last edited by Phil Jackson; 05-10-2010 at 02:05 PM.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • Users who have thanked Phil Jackson for this post:

    LJackson (05-10-2010)

  • #7
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    <?php

    $prodShortName 
    '56luke2\\\|||\\\|| ***&343act - webdsc5465--designs';
    $prodShortName preg_replace("#(?:\s+-\s?)|(?:\s?-\s+)#is"""preg_replace("#[^\w\d&;\s-]#is"""$prodShortName)); 
    echo 
    $prodShortName;

    # output:
    # 56luke2 56luke2 &343actwebdsc5465--designs

    ?>
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,098
    Thanks
    297
    Thanked 12 Times in 12 Posts
    thanks mate!!!

    what does the ?: mean? for future reference

    cheers
    Luke

  • #9
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    grouping a cluster together without returning in the resultset ($1) like:

    Code:
     ((?:pr)|(?:l)icking)
    will match:

    "pricking" and "licking"

    and will return $1 = "pricking" (or licking)

    but

    (pr|licking)

    will match "pricking" and "plicking" ( what ever that is ) but not "licking"
    Last edited by Phil Jackson; 05-10-2010 at 05:24 PM.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • Users who have thanked Phil Jackson for this post:

    LJackson (05-10-2010)


  •  

    Posting Permissions

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