PDA

Click to See Complete Forum and Search --> : preg_replace


RobDog888
Feb 15th, 2008, 04:31 AM
I need to replace a space delimited string in a db query with apostrophy comma apostrophy.

I have this but it pukes.

preg_replace('/ /', '', '', $myvar['keywords'])

Basically its ....

item1 item2 item3

And should be ...

item1', 'item2', 'item3

as I can easily wrap the external apostrophies around the result to make...

'item1', 'item2', 'item3'


What escape sequence do I need if any?

Thanks

penagate
Feb 15th, 2008, 05:22 AM
There is no need to use preg_replace. A simple str_replace will do it.

$items_list = 'item1 item2 item3';
$items = "'".str_replace(' ', "', '", trim($items_list))."'";

RobDog888
Feb 15th, 2008, 03:18 PM
I have this now but its not matching any results.

SELECT * FROM Table1
WHERE
(somefield = 1)
AND
(v.keywords IN('" . str_replace(' ', "', '", trim($myvar['keywords'])) . "'))

No errors but no results either. Could it be that the keywords field has a space delimited values in it? I just need to match any of those workds woith any that are in $myvar.