regex - Replace spaces in URL after # -
i have database dump wordpress has url's spaces need remove spaces from. thought lot simpler of task. expression match bad href's
(href\="http\:\/\/wfsu.org/blog-coastal-health\/\?page_id\=\d+\/\#)((\w+)\s(\w+))+\"
the problem trying replace spaces dashes ..
i'm replacing $1$3-$4"
works fine on 2 words not 3+ words. have solution?
i consider using preg_replace_callback()
. allow use regex grab url string interested in , pass callback function can use string manipulation replace spaces dashes.
your code might this:
$original_string; // original string holding text content $pattern = '~href="http://wfsu.org/blog-coastal-health/.*"~i'; $cleaned_string = preg_replace_callback( $pattern, function ($matches) { return str_replace(' ', '-', $matches[0]); }, $original_string );
this eliminates need try determine how many pattern fragments need replace within regex itself. instead capture entire href
property value , simple str_replace()
on it.
Comments
Post a Comment