简体   繁体   English

在php变量的两个条目之间查找条目

[英]Find entry between two entries in a php variable

I have a php variable that looks like this q1q32q1qq2q14q2qq3q16q3q. 我有一个看起来像这样的php变量q1q32q1qq2q14q2qq3q16q3q。 The logic of this variable is that an entry starts with for instance q1q and ends with q1q and in between those two entries there is a number, in this example 32. What I want is to write a code that finds the number in between given that you know q1q. 这个变量的逻辑是一个条目以例如q1q开始并以q1q结尾,并且在这两个条目之间有一个数字,在这个例子中是32个。我想要的是编写一个代码来查找给定的数字。你知道q1q。 This is what I started to write 这就是我开始写的

strpos($participants, "q" . $_SESSION["UserID"] . "q")

In accordance with the above example $_SESSION["UserID"] is equal to 1 and $participants is the variable containing q1q32q1qq2q14q2qq3q16q3q. 根据上面的例子,$ _SESSION [“UserID”]等于1,$ participant是包含q1q32q1qq2q14q2qq3q16q3q的变量。 Any ideas on how to complete my code? 关于如何完成我的代码的任何想法?

try preg_match 尝试preg_match

$str = 'q1q32q1qq2q14q2qq3q16q3q';
$word1 = 'q1q';
$word2 = 'q1q';
preg_match('/'.preg_quote($word1).'(.*?)'.preg_quote($word2).'/is', $str, $match);

print_r($match); //$match[1] will have desired output

output: 输出:

Array
(
    [0] => q1q32q1q
    [1] => 32
)

you can try with strpos and substr but it quite lengthy see below example: 您可以尝试使用strpossubstr但是很长,请参见以下示例:

echo substr($str, (strpos($str, $word1) + strlen($word1)), (strpos($str, $word2, 1) - strlen($word2))); //output: 32

你需要缓冲读取php文件并添加条件,如果在文件中找到字符串变量,然后根据需要操作它我不认为是任何其他方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM