我有一个数据集(纯文本),我想找到某些链接。我想要查找的链接始终以字符串的相同部分开头。如何创建包含所有结果的数组?
示例
Lorem ipsum dolor坐下来,精神上的精神。 Donec aliquet,nulla id vestibulum efficitur,link1.com/just/a/link/1 diam ligula pharetra libero,sed pulvinar link1.com/just/a/link/2 massa ante nec justo。 Phasellus tristique dignissim justo sed luctus。 Duis link2.com/just/a/link/1 vel scelerisque est.Vestibulum sit amet leo lacinia,link1.com / just / a / link / 2 iaculis mi ut,ullamcorper libero。
在这篇文章中,我有两个部分的某个字符串(link1.com/和link2.com/),之后我们有2个链接去剥离。
我知道如何在一个阵列中获得所有四个链接吗?
我试过这样的事情:
$data = str_replace('"', "", $data);
$data = str_replace("'", "", $data);
$data = str_replace("’", "", $data);
$data = str_replace("…", "", $data);
$data = strtolower($data);
$data = trim(preg_replace('/\s+/', ' ', $data));
$delimiter = "link1.com/just/a/link/";
$array = explode($delimiter, $data);
答案 0 :(得分:0)
<?php
$str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec aliquet, nulla id vestibulum efficitur, link1.com/just/a/link/1 diam ligula pharetra libero, sed pulvinar link1.com/just/a/link/2 massa ante nec justo. Phasellus tristique dignissim justo sed luctus. Duis link2.com/just/a/link/1 vel scelerisque est. Vestibulum sit amet leo lacinia, link1.com/just/a/link/2 iaculis mi ut, ullamcorper libero.";
$arr = explode(" ",$str);
$link = array();
foreach($arr as $word)
{
if(strpos($word,".com"))
{
array_push($link,$word);
}
}
print_r($link);
?>
<强>输出强>
Array
(
[0] => link1.com/just/a/link/1
[1] => link1.com/just/a/link/2
[2] => link2.com/just/a/link/1
[3] => link1.com/just/a/link/2
)