我如何在 php 中创建一个字符串对象数组?
how do i create an array of a string object in php?
我想创建一个变量数组 $link 来获取数组中的所有 link 以便我可以在花括号外同时处理它们
include("simple_html_dom.php");
$html = file_get_html($url);
$i=0;
$linkObjs = $html->find('h3.r a');
foreach ($linkObjs as $linkObj)
{
$title = trim($linkObj->plaintext);
$link = trim($linkObj->href);
//if it is not a direct link but url reference found inside it, then extract
if (!preg_match('/^https?/', $link) && preg_match('/q=(.+)&sa=/U', $link, $matches) && preg_match('/^https?/', $matches[1]))
{
$link = $matches[1];
} else if (!preg_match('/^https?/', $link)) { // skip if it is not a valid link
continue;
}
$descr = $html->find('span.st',$i); // description is not a child element of H3 thereforce we use a counter and recheck.
$i++;
}
创建数组并推送匹配项。
include("simple_html_dom.php");
$html = file_get_html($url);
$links = array();
$i=0;
$linkObjs = $html->find('h3.r a');
foreach ($linkObjs as $linkObj)
{
$title = trim($linkObj->plaintext);
$link = trim($linkObj->href);
// if it is not a direct link but url reference found inside it, then extract
if (!preg_match('/^https?/', $link) && preg_match('/q=(.+)&sa=/U', $link, $matches) && preg_match('/^https?/', $matches[1]))
{
array_push($links, $link);
} else if (!preg_match('/^https?/', $link)) { // skip if it is not a valid link
continue;
}
$descr = $html->find('span.st',$i); // description is not a child element of H3 thereforce we use a counter and recheck.
$i++;
}
只需声明一个数组变量,添加它以备后用。
循环之前,
$myLinks = [];
并且,就在这一行之后,
$link = $matches[1];
$myLinks[] = $link;
现在,您可以使用数组 $myLinks,希望这是您需要的。
我想创建一个变量数组 $link 来获取数组中的所有 link 以便我可以在花括号外同时处理它们
include("simple_html_dom.php");
$html = file_get_html($url);
$i=0;
$linkObjs = $html->find('h3.r a');
foreach ($linkObjs as $linkObj)
{
$title = trim($linkObj->plaintext);
$link = trim($linkObj->href);
//if it is not a direct link but url reference found inside it, then extract
if (!preg_match('/^https?/', $link) && preg_match('/q=(.+)&sa=/U', $link, $matches) && preg_match('/^https?/', $matches[1]))
{
$link = $matches[1];
} else if (!preg_match('/^https?/', $link)) { // skip if it is not a valid link
continue;
}
$descr = $html->find('span.st',$i); // description is not a child element of H3 thereforce we use a counter and recheck.
$i++;
}
创建数组并推送匹配项。
include("simple_html_dom.php");
$html = file_get_html($url);
$links = array();
$i=0;
$linkObjs = $html->find('h3.r a');
foreach ($linkObjs as $linkObj)
{
$title = trim($linkObj->plaintext);
$link = trim($linkObj->href);
// if it is not a direct link but url reference found inside it, then extract
if (!preg_match('/^https?/', $link) && preg_match('/q=(.+)&sa=/U', $link, $matches) && preg_match('/^https?/', $matches[1]))
{
array_push($links, $link);
} else if (!preg_match('/^https?/', $link)) { // skip if it is not a valid link
continue;
}
$descr = $html->find('span.st',$i); // description is not a child element of H3 thereforce we use a counter and recheck.
$i++;
}
只需声明一个数组变量,添加它以备后用。
循环之前,
$myLinks = [];
并且,就在这一行之后,
$link = $matches[1];
$myLinks[] = $link;
现在,您可以使用数组 $myLinks,希望这是您需要的。