【问题标题】:how to browse a string text word by word and replace it如何逐字浏览字符串文本并替换它
【发布时间】:2016-04-21 07:30:25
【问题描述】:

我有一个这样的 php 数组

$values = array(
"hello" => "12",
"a" => "24",
"grand" => "40",
"mother" => "10"
);

和一个字符串,例如
$string = "<p>hello</p><div style="align:center"> i am a grand mother</span>";
初始文本可以包含 html 代码。这一点很重要。

我需要将字符串中的所有单词替换为 <span id="word_id">word</span>

在我的示例中,结果将是:

<p>
  <span id="12">hello</span>
</p>
<div style="align:center">i am 
  <span id="24">a</span>
  <span id="40">grand</span>
  <span id="10">mother</span>
</div>

主要问题是它可以用于基本的 string_replace,因为它将替换单词“grand”中的单词“a”,例如。 另一件事,我需要在我的文本中保留初始 html 代码。 而且我不知道我是否可以浏览我的字符串文本并在存在单词的情况下逐字替换。

【问题讨论】:

  • 我认为这就是您所需要的。 stackoverflow.com/questions/3426265/…
  • $string = "你好,我是祖母"; $string = preg_replace('/\s+/', ' ', $string);
  • HTML 代码现在中断了字符串分配。您需要查看混合单曲 ` ` 和 "

标签: php html regex


【解决方案1】:

这里不需要正则表达式。 PHP 有一个用于标记字符串的功能(将它们分成更小的字符串)

此方法还利用array_keys() 返回$values 数组中的键数组。

<?php

$values = array(
    "hello" => "12",
    "a" => "24",
    "grand" => "40",
    "mother" => "10"
    );

$string = "hello I am a grand mother";
$token = strtok($string, " ");
$newString = '';

while ($token !== false) {
    if (in_array($token, array_keys($values))) {
        $newString .= "<span id='" . $values[$token] . "'>". $token . "</span> ";
    } else {
        $newString .= " " . $token . " ";
    }
    $token = strtok(" ");
}

echo $newString;
?>

【讨论】:

  • 谢谢你的回答!!!抱歉,我编辑了我的帖子。我的初始文本包含 html 代码..我需要保留它..您的代码适用于此吗?谢谢!
  • @philou75015 如果您的编辑建议将字符串拆分为多行,您可以将换行符添加到令牌输入中。
  • @philou75015 标记化方法依赖于您的单词被空格分隔。我看不到您添加的 HTML 标记。请编辑问题以使用单个 ` 反引号 ` 将字符串括起来并使其格式化为 code
  • 我稍后看看,看看这种方法是否可以容纳添加的 HTML
【解决方案2】:

这是我的解决方案:

<?php
$string = "hello i am a grand mother"; 

$values = array(
    "hello" => "12",
    "a" => "24",
    "grand" => "40",
    "mother" => "10"
);

foreach($values as $word=>$id){
    $string = preg_replace("/(\s|^)$word(\s|$)/","$1<span id=\"$id\">$word</span>$2",$string);  
}

echo $string;

【讨论】:

  • 感谢塞缪尔,它的工作正常,但不是在 2 种情况下。 1° 当我的单词在字符串文本中是大写字母时(数组中没有) 2° 当单词在特殊字符后面时(例如 $string = "你好,我是祖母和你的祖父";它可以是逗号,也可以是" 和其他特殊字符..) thx thx thx 提前!!!
【解决方案3】:

虽然解决的有点晚了。这里是:

$exp = explode(' ', $string);

foreach ($exp as $e) {
    if (array_key_exists($e, $values))
    {
        $a = "<span id=\"".$values[$e]."\">".$e."</span>";

    }
    else
    {
        $a = $e.' ';    
    }       
    $result .= $a;
}

echo $result;

【讨论】:

  • 这不会错过附加在数组中不存在的单词吗?对于 OP 示例 Iam
【解决方案4】:

使用explode() 尝试此代码,计算字符串单词并使用in_array 条件创建for 循环。

$values = array(
    "hello" => "12",
    "a" => "24",
    "grand" => "40",
    "mother" => "10"
);

$string = "

hello

i am a grand mother";
$string = preg_replace('/\s+/', ' ', $string);
$stringExp = explode(" ", $string); 

$len = count($stringExp);
$newString = '<p>';
for($i=0; $i<$len;$i++){
    if (in_array($stringExp[$i], array_keys($values))) {
        $newString .= "<span id='" . $values[$stringExp[$i]] . "'>". $stringExp[$i] . "</span> ";
    } else {
        $newString .= " " . $stringExp[$i] . " ";
    }   
    if ($i == 1) {
        $newString .= '</p><div style="align:center">';
    } else if ($i == $len - 1) {
         $newString .= "</div>";
    }   
}
echo $newString;

输出

//<p><span id="12">hello</span></p><div style="align:center">i am <span id="24">a</span><span id="40">grand</span><span id="10">mother</span></div>

【讨论】:

    猜你喜欢
    • 2015-01-23
    • 1970-01-01
    • 2010-09-12
    • 2023-04-08
    • 2023-04-04
    • 1970-01-01
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多