[英]Stripping two characters from a string PHP using regular expression
I have a list of html tags of the form我有一个表单的 html 标签列表
<a>
<div>
...
How would I strip the <>
away from the strings to return the word in between using regular expressions?我将如何从字符串中剥离<>
以使用正则表达式返回中间的单词?
Attempts:尝试:
preg_replace('~<>~', ' ', $tag);, preg_replace("[<>]", ' ', $tag);, and preg_replace(array(">","<"), " ", $tag);
Based on your examples, the problem is that you are not using delimiters.根据您的示例,问题在于您没有使用分隔符。 You must use a delimiter (typically /
, but often ~
).您必须使用分隔符(通常是/
,但通常是~
)。 From the manual :从手册:
When using the PCRE functions, it is required that the pattern is enclosed by delimiters.使用 PCRE 函数时,需要使用分隔符将模式括起来。 A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.分隔符可以是任何非字母数字、非反斜杠、非空白字符。
Often used delimiters are forward slashes (
/
), hash signs (#
) and tildes (~
).经常使用的分隔符是正斜杠 (/
)、井号 (#
) 和波浪号 (~
)。 The following are all examples of valid delimited patterns.以下是有效分隔模式的所有示例。
/foo bar/
#^[^0-9]$#
+php+
%[a-zA-Z0-9_-]%
So, your code could be:所以,你的代码可能是:
$newString = preg_replace("/[<>]/", ' ', $tag);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.