如何使用PHP获取特定字符串之间的内容 [英] How to get the content between the specific string using PHP

查看:279
本文介绍了如何使用PHP获取特定字符串之间的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例字符串:这是使用\title {test}标签测试字符串。



如果我想获得括号或括号之间的内容我可以使用下面提到的代码。

  $ text  = '  [This]是[test]字符串,[吃]我的[短裤]。'; 
preg_match_all( / \ [[^ \]] * \] /,$ text,$ matches);
var_dump($ matches [0]);



如果我要提取\title {和}之间的单词,那将是什么准确的正则表达式?

我尝试将此代码替换为[]和\ title {}但失败了。

  $ text  = '  [This]是[test]字符串,[吃]我的[短裤]。'; 
preg_match_all( / \\title {[^ \]] * \} / ,$ text,$ matches);
var_dump($ matches [0]);

解决方案

text = ' [This]是[test]字符串,[吃]我的[短裤]。';
preg_match_all( / \ [[^ \]] * \] /

文本,

匹配);
var_dump(


Example string: This is testing string with \title{test} tags.

If I want to get the content between the parenthesis or brackets I can use below mentioned code.

$text = '[This] is a [test] string, [eat] my [shorts].';
preg_match_all("/\[[^\]]*\]/", $text, $matches);
var_dump($matches[0]);


If I want to extract the words between "\title{" and "}",what would be the exact regex?
I tried this code by replacing "[ ]" with "\title{" "}" but failed.

$text = '[This] is a [test] string, [eat] my [shorts].';
preg_match_all("/\\title{[^\]]*\}/", $text, $matches);
var_dump($matches[0]);

解决方案

text = '[This] is a [test] string, [eat] my [shorts].'; preg_match_all("/\[[^\]]*\]/",


text,


matches); var_dump(


这篇关于如何使用PHP获取特定字符串之间的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆