PHP正则表达式问题
我有一个存储书页的字符串。它是这样的:
///0///
Page1 Text
///1///
Page2 Text
///2///
Page3 Text
///3///
我想提取页面文本(Page1 文本、Page2 文本、Page3 文本)。这是正在使用的正则表达式:
$format = "%///\d*///(.*)///\d*///%";
preg_replace_callback($format, "process_page", $text);
根据此页面 我可以在表达式的开头和结尾使用除 / 之外的其他字符。所以我使用 % 来简化我的模式,所以我不必使用像这样的转义字符 \/
对我来说似乎没问题,但它什么也不返回。有人可以告诉我问题出在哪里吗?
I have a string which I store book pages. It's something like this:
///0///
Page1 Text
///1///
Page2 Text
///2///
Page3 Text
///3///
I want to extract page texts (Page1 Text, Page2 Text, Page3 Text). Here is the Regular Expression which is am using:
$format = "%///\d*///(.*)///\d*///%";
preg_replace_callback($format, "process_page", $text);
According to this page I can use other character than / in the start and end of the expression. So I used % to simplify my pattern, so I don't have to use escape character like this \/
It seems okay to me, but it return nothing. Can somebody please tell me where is the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为
preg_split
对您来说可能是更好的选择:每个页面现在都有自己的数组元素。
I think
preg_split
might be a better option for you:Each page is now in it's own array element.
我认为您需要
s
修饰符:$format = "%///\d*///(.*)///\d*///%s";
我不确定你想做什么,但我个人不会为此使用正则表达式。您知道要查找的确切字符串(例如
///4///
),并从那里找到结束字符串(///5///< /code> 或文件结尾)。带有 strpos 的简单 substr 可能是更好的选择。
I think you need the
s
modifier:$format = "%///\d*///(.*)///\d*///%s";
I'm not sure what you're tryingto do but personally I wouldn't use regex for this. you know the exact string to look for (eg
///4///
) and from there the end string (///5///
or end of file). A simle substr with strpos might be a better option.我会使用类似
preg_spilt
的东西(参见 Tim Cooper 的回答)。但对于您的正则表达式,请尝试以下操作:
使用环视断言和
s
-修饰符。I would use something like
preg_spilt
(see Tim Cooper's answer).But for your RegEx, try this:
With Look-around assertion and
s
-modifier.