在PHP中没有空格的字符串中查找单词

时间:2022-10-29 16:54:03

I parse an XML file with PHP. My problem is I have a dynamic string with multiple lines and each line has no space char on it, and I want to find a word on that string. String's length is dynamic so it changes every time.

我用PHP解析XML文件。我的问题是我有一个包含多行的动态字符串,每行都没有空格字符,我想在该字符串上找到一个单词。字符串的长度是动态的,因此每次都会改变。

Since strings length is dynamic I can't use something like $c = substr($string, 0, -1) or I can't use something like $i=stripos($story," word"); because there is no space in the string.

由于字符串长度是动态的,我不能使用类似$ c = substr($ string,0,-1)的东西,或者我不能使用像$ i = stripos($ story,“word”)这样的东西;因为字符串中没有空格。

example string is 4 lines and I want to detect the word ARC.docx on second line before </a>:

示例字符串是4行,我想在之前检测第二行上的ARC.docx这个词:

<![CDATA[
In <a href='/home/Apps/ARCMeeting'>ARCMeeting</a>, You edited the file <a href='https://dropbox.com/get/Apps/ARCMeeting/ARC.docx?w=d3' title='&#47;Apps&#47;ARCMeeting&#47;ARC.docx'>ARC.docx</a>.
<br/>
]]>

My goal is add that ARC.docx on second line before </a> to a message body

我的目标是在消息体之前将第二行的ARC.docx添加到

$message='X File has been edited!';

so I can print

所以我可以打印

$message='ARC.docx File has been edited!';

How can I detect word that in between .......>exampleword</a>. in above string?

我怎样才能检测到.......> exampleword 之间的单词。在上面的字符串?

Thanks in advance

提前致谢

2 个解决方案

#1


2  

You can try preg_match function.

你可以尝试preg_match功能。

In your case it will look something like:

在您的情况下,它看起来像:

$subject = "In <a href='/home/Apps/ARCMeeting'>ARCMeeting</a>, You edited the file <a href='https://dropbox.com/get/Apps/ARCMeeting/ARC.docx?w=d3' title='&#47;Apps&#47;ARCMeeting&#47;ARC.docx'>ARC.docx</a>"

preg_match("/title=(.*)>(.*)<\/a>/U", $subject, $matches);

echo $matches[2]; // $matches[2] will contain `ARC.docx`

#2


2  

Use a DOM parser such as the one built into PHP.

使用DOM解析器,例如PHP内置的解析器。

$doc = new DOMDocument();

$html_string = <<<EOD
 <![CDATA[
 In <a href='/home/Apps/ARCMeeting'>ARCMeeting</a>, You edited the file <a href='https://dropbox.com/get/Apps/ARCMeeting/ARC.docx?w=d3' title='&#47;Apps&#47;ARCMeeting&#47;ARC.docx'>ARC.docx</a>.
 <br/>
 ]]>
EOD;

@$doc->loadHTML($html_string);

$urls = $doc->getElementsByTagName('a');

foreach ($urls as $url) {
 echo $url->nodeValue;
}

ARC.docx

ARC.docx

#1


2  

You can try preg_match function.

你可以尝试preg_match功能。

In your case it will look something like:

在您的情况下,它看起来像:

$subject = "In <a href='/home/Apps/ARCMeeting'>ARCMeeting</a>, You edited the file <a href='https://dropbox.com/get/Apps/ARCMeeting/ARC.docx?w=d3' title='&#47;Apps&#47;ARCMeeting&#47;ARC.docx'>ARC.docx</a>"

preg_match("/title=(.*)>(.*)<\/a>/U", $subject, $matches);

echo $matches[2]; // $matches[2] will contain `ARC.docx`

#2


2  

Use a DOM parser such as the one built into PHP.

使用DOM解析器,例如PHP内置的解析器。

$doc = new DOMDocument();

$html_string = <<<EOD
 <![CDATA[
 In <a href='/home/Apps/ARCMeeting'>ARCMeeting</a>, You edited the file <a href='https://dropbox.com/get/Apps/ARCMeeting/ARC.docx?w=d3' title='&#47;Apps&#47;ARCMeeting&#47;ARC.docx'>ARC.docx</a>.
 <br/>
 ]]>
EOD;

@$doc->loadHTML($html_string);

$urls = $doc->getElementsByTagName('a');

foreach ($urls as $url) {
 echo $url->nodeValue;
}

ARC.docx

ARC.docx