View Full Version : preg_replace advice please

11-04-2009, 03:28 PM
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

$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 :D

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

many thanks

Phil Jackson
11-04-2009, 05:53 PM
yes it is and elaberate more on the functuality of these regexps

11-04-2009, 06:08 PM
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

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


11-04-2009, 06:50 PM
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


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


05-10-2010, 02:49 PM
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

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


Phil Jackson
05-10-2010, 03:03 PM
EDIT: thats wrong... 2 mins..

Phil Jackson
05-10-2010, 03:16 PM

$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


05-10-2010, 06:12 PM
thanks mate!!!

what does the ?: mean? for future reference :)


Phil Jackson
05-10-2010, 06:22 PM
grouping a cluster together without returning in the resultset ($1) like:


will match:

"pricking" and "licking"

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



will match "pricking" and "plicking" ( what ever that is ) but not "licking"