homepage Welcome to WebmasterWorld Guest from 54.227.171.163
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
preg replace problem
compose

5+ Year Member



 
Msg#: 3061179 posted 9:53 am on Aug 26, 2006 (gmt 0)

Hello,

I want to replace all character other then alphanumeric to "-", to get this output i wrote this expression but it not works, please check it.

preg_replace([^a-zA-Z0-9],"-",$string);

Please help.

Vineet

 

barns101

5+ Year Member



 
Msg#: 3061179 posted 11:55 am on Aug 26, 2006 (gmt 0)

You may need to add a $ to the pattern, so that it knows to stop:


preg_replace([^a-zA-Z0-9$],"-",$string);

compose

5+ Year Member



 
Msg#: 3061179 posted 12:40 pm on Aug 26, 2006 (gmt 0)

Hello,

Thanks for reply. But it not works :(.

Vineet

barns101

5+ Year Member



 
Msg#: 3061179 posted 12:53 pm on Aug 26, 2006 (gmt 0)

Try this (not tested, but similar to something I use):


preg_replace("^([a-zA-Z0-9])$","-",$string);

coopster

WebmasterWorld Administrator coopster us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3061179 posted 1:49 pm on Aug 26, 2006 (gmt 0)

compose, your original pattern would have worked but you need to supply delimiters [php.net]. Also remember that the original string remains the same, you need to assign the replaced value to either the original variable or a new variable:
<pre> 
<?php
$string = 'abcABC123-@#4abcABC123';
print "$string\n";
$updatedString = preg_replace("/[^a-zA-Z0-9]/","-",$string);
print "$string\n";
print "$updatedString\n";
?>
</pre>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved