Get string at certain position after a string and append

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 <a class="class_to_use_here">Hebrews 9:28 </a>

<style>a {border-bottom:1px solid blue}</style>
<?php 
$string='check this passage Hebrews 9:28';
$find =$replacement= array();
$bibok=array('Hebrews','Genesis');
foreach($bibok as $bib){
    $find[]=$bib;
    $replacement[]='<a class="class_to_use_here">'.$bib.' </a>';
}
$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 <a class="class_to_use_here">Hebrews</a>9: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 <a class="class_to_use_here">Hebrews 9:28 </a>

I was able to achieve this only <a class="class_to_use_here">Hebrews</a> 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.

In general, you would use - PHP: preg_replace - Manual

Sorry to ask to much from you to help me with a solution that works. I don’t know how to use preg_replace at the moment.

It’s been a while since I did any regex, but the following should work -

// array of words to find and convert
$words = ['Hebrews', 'Genesis'];

$find = [];
foreach($words as $word)
{
	// definition - word boundary, start of capture, word, 0 or more spaces, 1 or more digits,
	// 0 or more :, 1 or more digits, 0 or more -, 1 or more digits, end of capture, word boundary
	$find[] = "/\b($word\s*\d+:*\d+-*\d+)\b/i";
}

// the $1 receives the matched value, inside the 1st/only capture (...) in the $find patterns
$replacement = '<a class="class_to_use_here">$1</a>';

echo preg_replace($find, $replacement, $string);
1 Like

To achieve the desired output where the entire string including the keyword and the number is wrapped in the HTML anchor tag, you can use preg_replace instead of str_ireplace. This way, you can match the entire pattern (the keyword followed by the number) and replace it in one go.

<style>
a {border-bottom:1px solid blue;}
</style>
<?php 
$string = 'check this passage Hebrews 9:28 and Genesis 1:2-3';
$bibok = array('Hebrews', 'Genesis');

// Create a regex pattern to match keywords followed by a number pattern
$pattern = '/(' . implode('|', $bibok) . ')(\s*\d+(:\d+(-\d+)?)?)/i';

$result = preg_replace_callback($pattern, function($matches) {
    return '<a class="class_to_use_here">' . trim($matches[1] . $matches[2]) . '</a>';
}, $string);

echo $result;
?>

Explanation:

  1. Regex Pattern:
    (’ . implode(’|’, $bibok) . '): Matches any keyword in $bibok (e.g., “Hebrews”, “Genesis”).
    (\s*\d+(:\d+(-\d+)?)?): Matches a number pattern that can include:
    An optional space (\s*)
    One or more digits (\d+)
    An optional colon and additional digits (:\d+)
    An optional range (-\d+)

  2. preg_replace_callback:
    This function is used to allow for dynamic replacement based on the matches.
    The callback function receives the matched results, and you construct the replacement string by concatenating the keyword and the number.

This implementation will give you the desired output like Hebrews 9:28 for the input string. Adjust the $string variable to test with different inputs!

Sponsor our Newsletter | Privacy Policy | Terms of Service