Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 粗体行标题需要正则表达式帮助_Php_Regex - Fatal编程技术网

Php 粗体行标题需要正则表达式帮助

Php 粗体行标题需要正则表达式帮助,php,regex,Php,Regex,我正在尝试在以下字符串/文本块的行标题中添加粗体/强标记: 长度测定:尺寸 上面显示中心到中心的位置 长度可以通过 所示的连接连杆长度 外球面杆端 材料:铝 特价:特价可在 任何卷 标准:MCP提供 但是我不能让正则表达式包含多个单词。以下是我的结果: 长度测定:尺寸 上面显示中心到中心的位置 长度可以通过 所示的连接连杆长度 外球面杆端 材料:铝 特价:特价可在 任何卷 标准:MCP提供 请注意,长度确定中的单词长度不是粗体/粗体。这需要包括在内。谁能帮我个忙吗 我的代码: $str = pr

我正在尝试在以下字符串/文本块的行标题中添加粗体/强标记:

长度测定:尺寸 上面显示中心到中心的位置 长度可以通过 所示的连接连杆长度 外球面杆端

材料:铝

特价:特价可在 任何卷

标准:MCP提供

但是我不能让正则表达式包含多个单词。以下是我的结果:

长度测定:尺寸 上面显示中心到中心的位置 长度可以通过 所示的连接连杆长度 外球面杆端

材料:铝

特价:特价可在 任何卷

标准:MCP提供

请注意,长度确定中的单词长度不是粗体/粗体。这需要包括在内。谁能帮我个忙吗

我的代码:

$str = preg_replace_callback("/([^\s]+:)/i", 'makeStrong', $str);

function makeStrong($matches) {
    return "<strong>" . $matches[0] . "</strong>"; 
}
试试这个:

<?
$str = "
Length Determination: the dimensions above show what center-to-center length can be achieved by the connecting linkage length shown for male spherical rod ends.
Material: aluminum.
Specials: specials are available at any volume.
Standards: MCP offers
";

$str = preg_replace_callback("/^([\w ]+:)/mi", 'makeStrong', $str);

function makeStrong($matches) {
    return "<strong>" . $matches[0] . "</strong>"; 
}

print $str;
?>
您的方法失败,因为[^\s]不匹配空格-这意味着它不匹配单词之间的空格

编辑固定的

尝试以下操作:

<?
$str = "
Length Determination: the dimensions above show what center-to-center length can be achieved by the connecting linkage length shown for male spherical rod ends.
Material: aluminum.
Specials: specials are available at any volume.
Standards: MCP offers
";

$str = preg_replace_callback("/^([\w ]+:)/mi", 'makeStrong', $str);

function makeStrong($matches) {
    return "<strong>" . $matches[0] . "</strong>"; 
}

print $str;
?>
您的方法失败,因为[^\s]不匹配空格-这意味着它不匹配单词之间的空格

EDIT Fixed

您的[^\s]字符类正在匹配任何非空白的字符-这显然意味着它不会在它们之间有空白的单词之间匹配。请尝试以下方法:

$str = preg_replace_callback("/^([\w\s]+?:)/i", 'makeStrong', $str);
您的[^\s]字符类正在匹配任何非空白的字符-这显然意味着它不会在它们之间有空白的单词之间匹配。请尝试以下方法:

$str = preg_replace_callback("/^([\w\s]+?:)/i", 'makeStrong', $str);
那么这个呢:

$str = preg_replace_callback("/([ \w]+:)/", 'makeStrong', $str);
如果模式以^开头,它将只匹配在整个字符串开头找到的内容。你的问题听起来像是你想一次处理多行字符串

如果\s与\w一起使用,它也将匹配换行符,并将它们包含在中,您可能已经注意到了。

这是怎么回事:

$str = preg_replace_callback("/([ \w]+:)/", 'makeStrong', $str);
如果模式以^开头,它将只匹配在整个字符串开头找到的内容。你的问题听起来像是你想一次处理多行字符串


如果将\s与\w一起使用,它也将匹配换行符,并将它们包括在中,正如您可能注意到的那样。

这与我的原始:preg_replace_callback/^[\w\s]+?:|[^\s]+:/i,“makeStrong”,“$str;这与我原来的:preg_replace_callback/^[\w\s]+?:|[^\s]+:/i,'makeStrong',$str;答对 了你搞定了,超小的。玩得好,宾果!你搞定了,超小的。打得好。