LJackson
11-04-2009, 02: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
Luke
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
Luke