Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.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,我想将:前面的字符串中的所有内容都加粗,并按以下方式执行(PHP): $string=preg_replace(“/(.*):/”、“\\1:”、$string); 现在我需要一个例外:如果在:之前有一个(),我不想加粗文本。这可能吗 (因此基本上,Size:123应该加粗,但是我们增加了这个(最大值:30)不应该加粗)当然,只要使用: $string = preg_replace('/([^(]*):/', '<span class="bold">\\1:</span>

我想将
前面的字符串中的所有内容都加粗,并按以下方式执行(PHP):

$string=preg_replace(“/(.*):/”、“\\1:”、$string);
现在我需要一个例外:如果在
之前有一个
),我不想加粗文本。这可能吗

(因此基本上,
Size:123
应该加粗,但是
我们增加了这个(最大值:30)
不应该加粗)

当然,只要使用:

$string = preg_replace('/([^(]*):/', '<span class="bold">\\1:</span>', $string);
$string=preg_replace('/([^(]*):/','\\1:',$string);

它将匹配:除(.

您将要中断的字符之前的任何字符…假设:所有字符

<a href="http://example.com"><img src="http://example.com/image.png"/></a>

最好将其作为数组放在第一位,如
数组('Size'=>'123')
@blue112几乎就在那里了,但为了不在字符串
中使用粗体
Max
,我们增加了它(Max:30)
需要确保整个字符串不包含任何开头括号。这可以通过添加字符串标记的开头来完成。
^

$string = preg_replace('/^([^(]*):/', '<span class="bold">\\1:</span>', $string);
$string=preg_replace('/^([^(]*):/','\\1:',$string);

看看它的作用。

现在怎么办?//更好的是:
:)没有任何东西会被破坏,html在那个地方是不允许的。而且它只用于输出,不用于存储。数据存储正确,对于这种文本,我不能有“大小->30”这样的结构因为每个数据集的属性都不同。@user2015253这没有解释,为什么不能使用数组而不是纯文本字符串(
array(
array('fooName'=>'barValue')
),那太好了,谢谢。如果我现在不想加粗原始示例中的
Max:
,我该怎么做(因为现在是“Max:”(加粗)
list($name, $value) = array_map('trim', explode(':', $attribute));
echo "<span class=\"bold\">$name</span>: $value";
$string = preg_replace('/^([^(]*):/', '<span class="bold">\\1:</span>', $string);