DEV Community

Oladele Oladipupo
Oladele Oladipupo

Posted on

pre_replace and regex php search text

I have got to replace a string with an html string. The string contain a certain keyword ( example: Hebrews, Genesis) and some number with symbol at the back (9:28 or 1:2-3). I want to replace the string with html equivalent from database and put the number within the HTML. .

The final result will be like Hebrews 9:28

a {border-bottom:1px solid blue}

<?php
$string='check this passage Hebrews 9:28';
$find =$replacement= array();
$bibok=array('Hebrews','Genesis');
foreach($bibok as $bib){
$find[]=$bib;
$replacement[]=''.$bib.' ';
}
$result = str_ireplace($find, $replacement, html_entity_decode($string));
echo $result;
?>
NOTE:$bibok is a associative array generated from a database with column . It works well for to bring Hebrews9:28. Note the string at the back. I want it inside the html element not outside. And the vary could be 9:28-29 in structure not always 9:28. And the value is dynamic depending on what the user type.

The final result will be like Hebrews 9:28

I was able to achieve this only Hebrews 9:28. But, I want the numbers structure inside the html element not outside. I can only use this simple str_ireplace. I will be glad to have preg_replace solution if it can be easily implemented in this case.

I am currently reading about pre_replace. After that I read regex yet I am not able to solve this problem nor find a way to apply all the available information on stackoverflow to solve the problem. Can any one help? Can I find an answer to a question this time around here?

Top comments (0)