【问题标题】:How to extract a single letter or number from a string?如何从字符串中提取单个字母或数字?
【发布时间】:2017-01-17 18:21:33
【问题描述】:

我需要从字符串中提取特定的字母或数字。

<div id="craftysyntax_1" style="float: right;"><script type="text/javascript" src="https://livehelp.clipboards.com/livehelp_js.php?eo=0&amp;department=1&amp;serversession=1&amp;pingtimes=10&amp;dynamic=Y&amp;creditline=W"></script></div>

从这个 divid="craftysyntax_1" 我想只从这个 craftysyntax_1 中提取数字 1,我正在尝试爆炸,但它对我不起作用,或者我做错了什么。

这是我尝试过的:

$myString = '<div id="craftysyntax_1" style="float: right;"><script type="text/javascript" src="https://livehelp.clipboards.com/livehelp_js.php?eo=0&amp;department=1&amp;serversession=1&amp;pingtimes=10&amp;dynamic=Y&amp;creditline=W"></script></div>';
            $strArray = explode('craftysyntax_', $myString, 1);
            print_r($myString);

我怎样才能实现我想要的?

【问题讨论】:

  • a regEx 似乎是理想的解决方案

标签: php explode


【解决方案1】:

您可以使用regEx 来实现您的目标,如本例所示

$myString = '<div id="craftysyntax_123" style="float: right;"><script type="text/javascript" src="https://livehelp.clipboards.com/livehelp_js.php?eo=0&amp;department=1&amp;serversession=1&amp;pingtimes=10&amp;dynamic=Y&amp;creditline=W"></script></div>';
$pttn = '@craftysyntax_(\d{1,})@';
preg_match( $pttn, $myString, $matches );

echo '<pre>',print_r($matches,1),'</pre>';

这将输出:

Array
(
    [0] => craftysyntax_123
    [1] => 123
)

因此您可以使用 $matches[1] 显式定位整数

【讨论】:

    【解决方案2】:
    preg_match("/craftysyntax_(.*)\\" /", $myString, $output_array);
    echo $output_array[1];
    

    祝你好运!

    【讨论】:

      【解决方案3】:

      我只想从这个 craftysyntax_1

      中提取数字 1

      使用preg_match函数:

      $myString = '<div id="craftysyntax_1" style="float: right;"><script type="text/javascript" src="https://livehelp.clipboards.com/livehelp_js.php?eo=0&amp;department=1&amp;serversession=1&amp;pingtimes=10&amp;dynamic=Y&amp;creditline=W"></script></div>';
      
      preg_match("/id=[\"']craftysyntax_(\d+)[\"']/", $myString, $matches);
      $craft_number = $matches[1];
      
      print_r($craft_number);  // 1
      

      【讨论】:

        【解决方案4】:

        使用 preg_match 试试这个:

        $myString = '<div id="craftysyntax_1" style="float: right;"><script type="text/javascript" src="https://livehelp.clipboards.com/livehelp_js.php?eo=0&amp;department=1&amp;serversession=1&amp;pingtimes=10&amp;dynamic=Y&amp;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
        

        【讨论】:

          【解决方案5】:

          你真的是shouldn't use regex to try and parse HTML

          另一种解决方案,使用DOMDocument 提取您想要的ID:

          $str = '<div id="craftysyntax_1" style="float: right;"><script type="text/javascript" src="https://livehelp.clipboards.com/livehelp_js.php?eo=0&amp;department=1&amp;serversession=1&amp;pingtimes=10&amp;dynamic=Y&amp;creditline=W"></script></div>';
          
          $dom = new DOMDocument;
          $dom->loadHTML($str);
          $src = $dom->getElementsByTagName('div')->item(0);
          $attr = $src->attributes->getNamedItem('id')->value;
          

          这将为您提供值craftysyntax_1。现在您可以轻松地抓取 last 下划线之后的所有内容(如果存在):

          if (($pos = strrpos($attr, '_')) !== false) {
              echo substr($attr, $pos + 1);
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-07-30
            • 1970-01-01
            • 1970-01-01
            • 2021-12-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多