Php 如何从字符串中提取单个字母或数字?
我需要从字符串中提取一个特定的字母或数字Php 如何从字符串中提取单个字母或数字?,php,explode,Php,Explode,我需要从字符串中提取一个特定的字母或数字 <div id="craftysyntax_1" style="float: right;"><script type="text/javascript" src="https://livehelp.clipboards.com/livehelp_js.php?eo=0&department=1&serversession=1&pingtimes=10&dynamic=Y&
<div id="craftysyntax_1" style="float: right;"><script type="text/javascript" src="https://livehelp.clipboards.com/livehelp_js.php?eo=0&department=1&serversession=1&pingtimes=10&dynamic=Y&creditline=W"></script></div>
我怎样才能实现我想要的
preg_match("/craftysyntax_(.*)\\" /", $myString, $output_array);
echo $output_array[1];
祝你好运 您可以使用
regEx
来完成本例中的目标
$myString = '<div id="craftysyntax_123" style="float: right;"><script type="text/javascript" src="https://livehelp.clipboards.com/livehelp_js.php?eo=0&department=1&serversession=1&pingtimes=10&dynamic=Y&creditline=W"></script></div>';
$pttn = '@craftysyntax_(\d{1,})@';
preg_match( $pttn, $myString, $matches );
echo '<pre>',print_r($matches,1),'</pre>';
因此,您可以使用$matches[1]
我只想从这个craftysyntax\u 1
使用preg_match
功能:
$str = '<div id="craftysyntax_1" style="float: right;"><script type="text/javascript" src="https://livehelp.clipboards.com/livehelp_js.php?eo=0&department=1&serversession=1&pingtimes=10&dynamic=Y&creditline=W"></script></div>';
$dom = new DOMDocument;
$dom->loadHTML($str);
$src = $dom->getElementsByTagName('div')->item(0);
$attr = $src->attributes->getNamedItem('id')->value;
$myString='';
preg\u match(“/id=[\”]craftysyntax(\d+[\“']/”,$myString,$matches);
$craft_number=$matches[1];
打印($craft_number);//1.
你真的喜欢
另一种解决方案是,使用DOMDocument
提取所需的ID:
if (($pos = strrpos($attr, '_')) !== false) {
echo substr($attr, $pos + 1);
}
使用preg_match尝试此操作:
$myString='';
preg\u match(“/id=[\”]craftysyntax(\d+[\”]/”,$myString,$output);
打印(输出)//数组([0]=>id=“craftysyntax\u 1”[1]=>1)
打印(输出[1])//1.
aregEx
似乎是理想的解决方案
$myString = '<div id="craftysyntax_1" style="float: right;"><script type="text/javascript" src="https://livehelp.clipboards.com/livehelp_js.php?eo=0&department=1&serversession=1&pingtimes=10&dynamic=Y&creditline=W"></script></div>';
preg_match("/id=[\"']craftysyntax_(\d+)[\"']/", $myString, $matches);
$craft_number = $matches[1];
print_r($craft_number); // 1
$str = '<div id="craftysyntax_1" style="float: right;"><script type="text/javascript" src="https://livehelp.clipboards.com/livehelp_js.php?eo=0&department=1&serversession=1&pingtimes=10&dynamic=Y&creditline=W"></script></div>';
$dom = new DOMDocument;
$dom->loadHTML($str);
$src = $dom->getElementsByTagName('div')->item(0);
$attr = $src->attributes->getNamedItem('id')->value;
if (($pos = strrpos($attr, '_')) !== false) {
echo substr($attr, $pos + 1);
}
$myString = '<div id="craftysyntax_1" style="float: right;"><script type="text/javascript" src="https://livehelp.clipboards.com/livehelp_js.php?eo=0&department=1&serversession=1&pingtimes=10&dynamic=Y&creditline=W"></script></div>';
preg_match("/id=[\"']craftysyntax_(\d+)[\"']/", $myString, $output);
print_r($output); //Array ( [0] => id="craftysyntax_1" [1] => 1 )
print_r($output[1]);//1