2

i have an php array like this

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

and a string for example
$string = "<p>hello</p><div style="align:center"> i am a grand mother</span>";
Initial text can contains html code. This point is important.

I need to replace all words in string by <span id="word_id">word</span>

In my example, the result will be:

<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>

The main problem is that it can used for a basic string_replace because it will replaced the word "a" in word "grand" for example . Another things, I need to keep initial html code on my text. And I don't know if I can browse my string text and replace word by word if word exist.

Rao
  • 20,781
  • 11
  • 57
  • 77
philou13010
  • 69
  • 1
  • 8

4 Answers4

3

You don't need a regex here. PHP has a function for tokenizing strings (splitting them into smaller strings)

This approach also makes use of array_keys() to return an array of the keys in your $values array.

<?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;
?>
Luke
  • 3,481
  • 6
  • 39
  • 63
  • thx for your answer !!! sorry but i edit my post. my initial text contains html code .. i need to keep it .. your code works for that ? thx ! – philou13010 Jan 15 '16 at 17:16
  • @philou75015 if as your edit suggests the string is split over multiple lines, you can add the newline character to the token input. – Luke Jan 15 '16 at 17:19
  • @philou75015 the tokenizing approach relied on your words being seperated by a space. I can't see the HTML markup you've added. Please edit the question to use single ` backticks ` to enclose the string and make it formatted as `code` – Luke Jan 15 '16 at 17:23
  • I'll take a look later and see if this approach can accommodate the added HTML – Luke Jan 18 '16 at 11:06
1

This was my solution:

<?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;
Samuel Cook
  • 16,620
  • 7
  • 50
  • 62
  • thx Samuel, its works fine but not in 2 cases . 1 ° when my word has an uppercase in string text ( and no in array ) 2° when word is behind special caractere ( example $string = "hello, i am a grand mother and your grand father"; it can be comma but also " and other special caracter .. ) thx thx thx in advance !!! – philou13010 Jan 16 '16 at 22:58
1

Made a solution a little bit late though.Here it is:

$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;
pritesh
  • 2,162
  • 18
  • 24
  • wouldn't this miss out appending words that don't exist in the array ? for the OP example `I` and `am` ? – Luke Jan 15 '16 at 17:15
0

Try this code using explode(), count string word and create for loop with in_array condition.

$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;

Outpur

//<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>
Jakir Hossain
  • 2,457
  • 18
  • 23