I need to convert lib_someString to <a href="someString">someString</a> inside a block of text using str_replace [not regex].
Here’s an example to give an exact sense what I mean: lib_12345 => <a href="12345">12345</a>. I need to do this for a bunch of instances in a block of text.
Below is my attempt. Problem I’m getting is that my function is not doing anything (I just get lib_id returned).
function extractLibId($val){ // function to get the "12345" in the above example
$lclRetVal = substr($val, 5, strlen($val));
return $lclRetVal;
}
function Lib($text){ // does the replace for all lib_ instances in the text
$lclVar = "lib_";
$text = str_replace($lclVar, "<a href='".extractLibId($lclVar)."'>".extractLibId($lclVar)."</a>", $text);
return $text;
}
Regexp gonna be faster and more clear, you will have no need to call your function for every possible ‘lib_’ string:
Without regexp, but every time Lib2 will be called somewhere will die cute kitten:
Use preg_replace.