PHP:返回两个字符之间的字符串 [英] PHP: Return string between two characters
问题描述
我想在大字符串中使用关键字".这些关键字以 my_keyword 开头和结尾,并且是用户定义的.如何在一个大字符串中搜索并找到两个*字符之间的含义并返回每个实例?
I am wanting to use "keywords" within a large string. These keywords start and end using my_keyword and are user defined. How, within a large string, can I search and find what is between the two * characters and return each instance?
它可能会更改的原因是,部分关键字可以由用户定义,例如 page_date_Y ,它可能会显示创建页面的年份.
The reason it might change it, that parts of the keywords can be user defined, such as page_date_Y which might show the year in which the page was created.
因此,再次,我只需要进行搜索并返回那些*字符之间的内容.这可能吗,或者如果我不知道关键字"的长度或我可能是什么样,有没有更好的方法呢?
So, again, I just need to do a search and return what is between those * characters. Is this possible, or is there a better way of doing this if I don't know the "keyword" length or what i might be?
推荐答案
<?php
// keywords are between *
$str = "PHP is the *best*, its the *most popular* and *I* love it.";
if(preg_match_all('/\*(.*?)\*/',$str,$match)) {
var_dump($match[1]);
}
?>
输出:
array(3) {
[0]=>
string(4) "best"
[1]=>
string(12) "most popular"
[2]=>
string(1) "I"
}
这篇关于PHP:返回两个字符之间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!