开发者

Regex fails with whitespace

开发者 https://www.devze.com 2023-01-05 18:36 出处:网络
$match = array(\'~(?:face\\=[\\\'\"]?(\\w开发者_如何学C+)[\\\'\"]?)~i\', \'~~i\'); $replace = array(\'font-family:$1;\', \'<span style=\"$1\">\');

$match = array('~(?:face\=[\'"]?(\w开发者_如何学C+)[\'"]?)~i', '~~i');

$replace = array('font-family:$1;', '<span style="$1">');

I want to replace the old <font> element which has a "face" attribute with a new <span> element with "style" attribute but the regex always fail when the font name in the "face" attribute contains whitespace, e.g : Courier New. How can I modify the regex to solve this problem?


Replace your \w+ with .+ to include all characters instead of just 'word' characters. Or all characters except ' like this: [^\']+

Updated version:

$match = array('~(?:face\=[\'"]?([^\']+)[\'"]?)~i', '~~i');
$replace = array('font-family:$1;', '<span style="$1">');
0

精彩评论

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