如何使用php在字符串中查找字符串
我正在访问mysql数据库并显示一列。此列中有一个长字符串,可以这样说:如何使用php在字符串中查找字符串,php,mysql,Php,Mysql,我正在访问mysql数据库并显示一列。此列中有一个长字符串,可以这样说: <image identifier="540aa2ad-9a8d-454d-b915-605b884e76d5"> <file><![CDATA[images/MV5BMTg5OTMxNzk4Nl5BMl5BanBnXkFtZTcwOTk1MjAwNQ@@._V1._SY317_CR0,0,214,317_.jpg]]></file> <title/>
<image identifier="540aa2ad-9a8d-454d-b915-605b884e76d5">
<file><![CDATA[images/MV5BMTg5OTMxNzk4Nl5BMl5BanBnXkFtZTcwOTk1MjAwNQ@@._V1._SY317_CR0,0,214,317_.jpg]]></file>
<title/>
<link/>
和.jpg]]>之间的任何内容都将在每一行上发生更改,我希望回显它们之间的任何内容
有人帮忙吗
谢谢
编辑
到目前为止,我已经:
function inStr ($needle, $haystack)
{
$needlechars = strlen($needle); //gets the number of characters in our needle
$i = 0;
for($i=0; $i < strlen($haystack); $i++) //creates a loop for the number of characters in our haystack
{
if(substr($haystack, $i, $needlechars) == $needle) //checks to see if the needle is in this segment of the haystack
{
return TRUE; //if it is return true
}
}
return FALSE; //if not, return false
}
$img = '
SELECT *
FROM `item`
';
$result0 = mysql_query($img);
while ($row0 = mysql_fetch_array($result0)){
$haystack = $row0['elements'];
$needle = '<![CDATA[images/';
}
if(inStr($needle, $haystack))
{
echo "string is present";
}
如果您有XML,那么很容易:
<?php
$xml = <<<END
<image identifier="540aa2ad-9a8d-454d-b915-605b884e76d5">
<file><![CDATA[images/MV5BMTg5OTMxNzk4Nl5BMl5BanBnXkFtZTcwOTk1MjAwNQ@@._V1._SY317_CR0,0,214,317_.jpg]]></file>
<title/>
<link/>
</image>
END;
$file = (string) simplexml_load_string($xml)->file;
echo $file;
如果您只向我们提供了部分文本blob,则可能需要稍微调整simplexml路径。您让它听起来像是XML中有几个图像,但由于我不知道它是如何形成的,所以我无法真正告诉您如何迭代它
哦,这也会返回.jpg部分,但是删除扩展名就像substr$file一样简单,如果你知道它是.jpg,那么0,-4。如果你搜索的内容是一致的,那么substr和strpos可能更好。请你举个例子,我以前没有使用过strpos。我使用了第二个正则表达式,尽管将其解析为xml对象也是一种选择。@reece:用谷歌搜索函数名的最小值即可生成php文档,并附有示例。@denis我了解函数的功能,但我不知道如何将它们结合使用以实现我想要的功能。这就是为什么我要从这里开始。使用strpos和substr到目前为止,我已经看到了编辑
<?php
$xml = <<<END
<image identifier="540aa2ad-9a8d-454d-b915-605b884e76d5">
<file><![CDATA[images/MV5BMTg5OTMxNzk4Nl5BMl5BanBnXkFtZTcwOTk1MjAwNQ@@._V1._SY317_CR0,0,214,317_.jpg]]></file>
<title/>
<link/>
</image>
END;
$file = (string) simplexml_load_string($xml)->file;
echo $file;