【问题标题】:Using Regex (or anything) to do an advanced find and replace使用正则表达式(或任何东西)进行高级查找和替换
【发布时间】:2013-08-21 12:55:06
【问题描述】:

我正在尝试查找双引号内的所有内容并将其替换为使用它的链接。我有超过 500 行的问题,所以我不想手动做。

原始php文档sn-p:

$q2 = array ("What does Mars look like from Earth?",
"What is Mars's position relative to Earth?");

$q3 = array ("What does Mars's surface look like?",
"Show me a view of the surface of Mars.",
"Show me a picture of the surface of Mars.");

我想要的格式:

$q2 = array ("<a href="answer.php?query=What+does+Mars+look+like+from+Earth%3F">What does Mars look like from Earth?</a>",
<a href="answer.php?query=What+is+Mars's+position+relative+to+Earth%3F">"What is Mars's position relative to Earth?");

我尝试使用正则表达式,但之前没有任何经验,我没有成功。使用 RegExr (my example) 我找到了:“[A-Za-z0-9\s.\?']*”并替换为:$&"

这只是给出了类似的结果

$q2 = array (<a href=answer.php?query="What does Mars look like from Earth?">"What does Mars look like from Earth?"</a>",

这很接近,但不是我需要的。希望有人知道我应该使用什么替换,或者更好的程序来尝试。任何帮助将不胜感激。

【问题讨论】:

  • 为什么不直接从你拥有的数组中动态构建你需要的数组呢?如果您不需要担心编写 HTML、正确地对字符串进行 URL 编码等,那么更新数组值会容易得多。只需编写一个函数,您可以通过该函数传入问题数组并接收数组的链接。顺便说一句,如果你想要正确格式化的 HTML,你应该在 href 值周围加上引号。
  • 也许您可以尝试使用空格作为分隔符进行拆分?并将每个拆分数组放回一个字符串。
  • 我不知道这意味着什么,但我愿意接受任何建议。我不需要使用正则表达式,这只是一个想法。
  • @Mike 我将如何编写一个函数来做到这一点?谢谢
  • 我认为这在单个正则表达式中是不可能的。正则表达式只会匹配实际存在的内容。它的“转换”能力停止在一个非常简单的替换功能上。您已经找到了搜索文本,但您需要第二个正则表达式来查找搜索文本中的空格并将它们替换为加号。

标签: php regex arrays replace


【解决方案1】:

我会通过如下函数运行它们。而不是使用正则表达式更新您的源代码。

function updateQuestions(&$questions){
    foreach($questions as $key => $value){
        $questions[$key] = '<a href="answer.php?query=' . urlencode($value) . '">' . $value . '</a>';
    }
}

updateQuestions($q2);

【讨论】:

  • 让我试试看,如果可行,我会回复你们。谢谢
  • 第二行出现语法错误。 (我试着把它放在一个 php 文档中并运行它)
【解决方案2】:

为什么不做一个这样的函数,你可以将你的数组传递给它并返回一个链接数组?

function make_questions_into_links($array) {
    if (!is_array($array)) {
        throw new Exception('You did not pass an array')
    } else if (empty($array)) {
        throw new Exception('You passed an empty array');
    }

    return array_map(function($element) {
        return '<a href="answer.php?query=' . urlencode($element) . '">' . $element . '</a>';
    }, $array);
}

【讨论】:

    【解决方案3】:

    以下代码应该可以工作:

    $q2 = array ('"What does Mars look like from Earth?"',
                 '"What is Mars\'s position relative to Earth?"'
                );
    $aq2 = preg_replace_callback(array_fill(0, count($q2), '/(?<!href=)"([^"]+)"/'),
          function($m){return '<a href="answer.php?query='.urlencode($m[1]).'">'.$m[1].'</a>';},
          $q2);
    
    // test the output
    print_r($aq2);
    

    输出:

    Array
    (
        [0] => <a href="answer.php?query=What+does+Mars+look+like+from+Earth%3F">What does Mars look like from Earth?</a>
        [1] => <a href="answer.php?query=What+is+Mars%27s+position+relative+to+Earth%3F">What is Mars's position relative to Earth?</a>
    )
    

    【讨论】:

      猜你喜欢
      • 2022-06-15
      • 1970-01-01
      • 2015-11-07
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      • 2017-03-28
      相关资源
      最近更新 更多