我是初学者,在php中,我想在我发现的第一个空格后使用正则表达式删除字符串。 我的代码就像:
<?php
$barcode = $_POST['barcode'];
$adress = "https://...";
$timeout = 40;
$ch = curl_init($adress);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
if (strpos($adress, 'https://') === 0) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page_content = curl_exec($ch);
$dom = new \DOMDocument('1.0', 'UTF-8');
$internalErrors = libxml_use_internal_errors(true);
$node = $dom->loadHTML($page_content);
$listeImages = $dom->getElementsByTagName('img');
foreach ($listeImages as $image)
{
$link = $dom->createElement("a");
$href = $dom->createAttribute('srcset');
$href->value = $image->getAttribute('srcset');
$value_img = print( $href -> nodeValue.'<br />');
$image->parentNode->replaceChild($link, $image);
$link->appendChild($href);
/*$exclusion_space_url=array(' ', ' 1x');
$url_cleaned=$this->$value_img;
foreach ($exclusion_space_url as $key=>$value) {
$url_cleaned=str_replace($value," ",$url_cleaned);*/ ***what i tried to do***
}
}
curl_close($ch);
?>
结果是:
一个html页面,其中有3-4行url serparate with space
感谢您的帮助
答案 0 :(得分:1)
$yourString = "Your String With Space";
$str= strtok($yourString,' ');
echo $str;