How can characters ” \n \t \r ” be replaced with ‘-‘ ?
echo preg_replace('/\s/','-','\n\t\n\r\n');//output '\n\t\n\r\n' instead should be'-----'
Edit: I have dynamic content in real app like:
preg_replace('/\s/','-',$_Request['content']);
can I fix it by adding “” around variable?
preg_replace('/\s/','-',"$_Request['content']");
Edit2:
How can be string converted from format ‘str’ to format “str”?
Thanks
Well, two things. First, the problem is single quotes in your replacement string. Meta-Characters (
\n\t\r, etc) are not processed inside of single quotes.However, don’t use a regex for this. There’s no need for the complexity of the regex. Use
Either use
str_replace:Or
strtr:Edit: Ahh, now I see what you’re getting at. You have a string with a literal
\r\n\t\r\v\n\tin it, and want to replace them out. Well, you can do that via regex:Basically, it matches any space character, and any literal
\followed by eitherr,n,torv…