gpt4 book ai didi

php - 正则表达式基础 : grab text between two constants

转载 作者:行者123 更新时间:2023-12-03 23:16:21 25 4
gpt4 key购买 nike

卡在 PHP 中的一个(相当简单的)正则表达式问题上。

这一部分隐藏在一堆文字中:

  <tr>
<td id="descriptionArea">
Customer request to remove "Intro - 01/13/09" video clip.
<br/>
</td>
</tr>

我想要介于两者之间的任何东西:

descriptionArea">

...和...

</td>

friend 建议:

$pattern = '<td="descriptionArea">\s*(.*?)\s*<';
$clean = preg_replace("'[\n\r\s\t]'","",$text); // to rid of line breaks
preg_match($pattern, $clean, $matches);
print_r($matches);

但是我得到以下错误:

Warning: preg_match() [function.preg-match]: Unknown modifier 'q'

我想第二个问题是 preg_match 是否也是正确的 PHP 函数。我应该改用 ereg 吗?感谢您的帮助。

最佳答案

使用 preg_* 时函数,第一个字符或模式被视为分隔符:

The expression must be enclosed in the delimiters, a forward slash (/), for example. Any character can be used for delimiter as long as it's not alphanumeric or backslash (\). If the delimiter character has to be used in the expression itself, it needs to be escaped by backslash. Since PHP 4.0.4, you can also use Perl-style (), {}, [], and <> matching delimiters.
Regular Expressions (Perl-Compatible) – Introduction

所以你不需要转义或替换&正如其他人所说的那样。而是使用适当的定界符并在表达式中转义这些字符:

'/&lt;td id=&quot;descriptionArea&quot;&gt;(.*?)&lt;\/td&gt;/'

关于php - 正则表达式基础 : grab text between two constants,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/584375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com