【问题标题】:how can I skip something in regex?如何在正则表达式中跳过某些内容?
【发布时间】:2019-05-22 13:30:28
【问题描述】:

我想查看所有字段输入和文本区域,但我不知道如何 在同一代码中一起显示。 如果我想跳过一些东西,例如我想写 直接输入名称而不让他检查类型,或者我不想让他显示只读,我该怎么做?

  <form method="post" action="" id="submit_form">

  <input type="text" name="TITLE" value="" size="40" maxlength="100" class="text" />

  <textarea name="DESCRIPTION" rows="4" cols="36" class="textarea"></textarea>

  <input type="text" name="DESCRIPTION_limit" size="4" class="limit_field" readonly="readonly" value="250" />

  <textarea name="ARTICLE" id="ARTICLE" rows="6" cols="50" class="textarea"></textarea>

  <input type="text" name="META_KEYWORDS" value="" size="40" maxlength="2000" class="text" /> 
preg_match_all('/<(input)[\s](type)="?([^>"]*)"?[\s](name)="?([^>"]*)"?[\s]/', file_get_contents($url), $matches);
echo"<pre>";
print_r($matches);

【问题讨论】:

标签: php regex curl


【解决方案1】:

我用“$string”行测试了这段代码。类型和名称属性在输入标签中可以是任意顺序,它会搜索直到读取到一个“>”字符。

    $string = '<input type="text" name="TITLE" value="" size="40" maxlength="100" class="text" />';
    preg_match_all('/<input[^>]*?(?:(type)="([^"]*)")|(?:(name)="([^"]*)")/', $string, $matches);
    if($matches[1][0] == "type" && $matches[3][1] == "name"){
    $type = $matches[2][0];
    $name = $matches[4][1];
    }
    elseif($matches[1][0] == "name" && $matches[3][1] == "type"){
    $name = $matches[2][0];
    $type = $matches[4][1];
    }
    else{
        throw new Exception('no input tags with type and name attributes were found!');
    }
    echo $type;
    echo $name;

【讨论】:

  • 此代码不起作用....我不想这样做,例如我想查找每个代码都有名称或输入名称,但我需要他忽略类型,因为在 preg_match 中,他按顺序逐步开始比赛
猜你喜欢
  • 2017-07-24
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
  • 2022-06-15
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
相关资源
最近更新 更多