开发者

How to Remove Space Additionally to the Special Chars?

in my functions.php if have this code:

echo '<a href="'.preg_replace('/\s/','-',$search).'-keyword1.html">'.urldecode($search).'</a>';

this removes the special chars..

but how can i additonally add re开发者_StackOverflowmove space and replace it with - and remove "

so, if someone types in "yo! here" i want yo-here


Try:

<?php

$str = '"yo! here"';

$str = preg_replace( array('/[^\s\w]/','/\s/'),array('','-'),$str);

var_dump($str); // prints yo-here

?>


If you want to replace a run of unwanted characters with a single dash, then you can use something like this:

preg_replace('/\W+/', '-', $search);

To remove surrounding quotes, and then replace any other junk with dashes, try this:

$no_quotes = preg_replace('/^"|"$/', '', $search);
$no_junk = preg_replace('/\W+/', '-', $no_quotes);


This will replace multiple spaces / "special" chars with a single hyphen. If you don't want that, remove the "+".

You might want to trim off any trailing hyphens, should something end with an exclamation point / other.

<?php preg_replace("/\W+/", "-", "yo! here   check this out"); ?>


You can remove any non-words:

preg_replace('/\W/', '-', $search);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜