Forum Moderators: coopster

Message Too Old, No Replies

preg_replace() Unknown modifier 'a'

         

xbl01234

9:12 am on Aug 31, 2007 (gmt 0)

10+ Year Member



Hi;
Could anyone debug for me, please.

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'a' in index.php on line 6

my code as following;

<?php //line 1

$str=' test & me ';
$p='^[^a-z]+$';
$r='_';
$str=preg_replace($p, $r, $str); // here is line 6
echo $str;

?>

cmarshall

10:21 am on Aug 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this:

$p='/^[^a-z]+$/';

xbl01234

10:36 am on Aug 31, 2007 (gmt 0)

10+ Year Member




$p='/^[^a-z]+$/';

The compiler does not complains now.

If i want to replace all the chars which are not belong to a-z, and more than one, by using '_', how i can do that, my code does not work for that. Could you help me, please.

For example;
' test & 900 = me '
become to
'_test_me_'

cmarshall

11:42 am on Aug 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe this (I cannot easily test right now):

$out_string = preg_replace("/\W+/", "_", $in_string);

Here [php.net] is a useful site for this kind of thing.

[edited by: coopster at 2:32 pm (utc) on Aug. 31, 2007]
[edit reason] changed link to authoritative domain [/edit]

xbl01234

11:54 am on Aug 31, 2007 (gmt 0)

10+ Year Member



Thanks a lot,that works for me.