分开单词并将其放入数组?

时间:2017-12-07 11:15:47

标签: php

User-agent: * Disallow: / kadirgamar/ Disallow: / arrow/ Disallow: / orion/ Disallow: / woolimlanka/ Disallow: / nippon/ Disallow: / luna-tower/ Disallow: / thotalagala/ Disallow: / altair/ Disallow: / arrowlibrary/ Disallow: / ashraf/ Disallow: / ashrafnew/ Disallow: / invoke/ Disallow: / lavana/ Disallow: / leia/ Disallow: / lorelle/ Disallow: / lioc/ Disallow: / liocdev/ Disallow: / lorellenew/ Disallow: / openmanthri/ Disallow: / opentapes/ Disallow: / rates/ Disallow: / rsq/ Disallow: / signature/ Disallow: / site/ Disallow: / visualretale/ Disallow: / srhr/ Disallow: / smitratest/ Disallow: / smitra/ Disallow: / lioccontent/ Disallow: / lioccopy/ Disallow: / liocold/ Disallow: / global/ Disallow: / echelon/ Disallow: / arrowlive/

这是我的字符串。我必须将kadirgamar,arrow等这些单词分开。如何在php中分离并将其放入数组?

我试过了: -

<?php 
$url = 'http://www.test.oddly.co/robots.txt';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, 1);
$result = curl_exec($curl);
curl_close($curl);
echo $result.'<br>';
$arr = array("lorelle","kadirgamar","arrow","orion","woolimlanka","nippon","luna-tower","thotalagala","arrowlibrary",
    "ashrafnew","invoke","lavana","leia","lorelle","lioc","liocdev","lorellenew","openmanthri","rsq","signature",
    "site","visualretale","srhr","smitratest","smitra","lioccontent","lioccopy","liocold","global","echelon","arrowlive"
);

$str = explode('Disallow: /',$result,-1);

?>

我试过这个,但它给出了错误。 我想检查两个数组是否相等?我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

你可以这样做: -

$array = explode('Disallow: / ',$vstring); //explode string to array

unset($array[0]); // to remove first word User-agent: * 
$array = array_values($array); // re-index array

function trim_array_element(&$item1)
{
    $item1 = preg_replace('/\//', '', $item1);
}

array_walk($array,'trim_array_element'); // apply custom function to remove / from each values of array

print_r($array);

输出: - https://eval.in/914410