Reputation: 55
I have function:
function postNumberToLink($string) {
return preg_replace('!\>\>\d+!', "<a href='#$0'><font color='red'>$0</font></a>", $string);
}
My questions:
What's better (generally): Formatting string before putting it to db, or formatting clear text after querying it from database?
I'd like to change every element compatibile with pattern ($0) in function above which means to remove first two characters (>>) and put the rest into href. I was trying to use substr on it, but it crashed. I also read about preg_replace_callback where I did put function as a second parameter and it also crashed even when I write inside the function the simpliest code. How can I make text like
"I'm replying to post no. >>21313123"
to "I'm replying to post no. [href=21313123][red]>>21313123[/red][/href]"
?
Sorry for brackets[], but I can't figure out how do SO tags work.
Regards Matt
Upvotes: 1
Views: 137
Reputation: 175038
Keep it in your syntax in the database. This has several advantages:
You can use control groups in your regular expression:
return preg_replace('!\>\>(\d+)!', "<a href='#$1'><font color='red'>$0</font></a>", $string);
// ^ ^ Brackets! ^^ First group ^^ All match
Don't use <font>
it's deprecated since before Jon Skeet had less than 100k reputation. Instead, use a class
name on your links, and use CSS to style those:
<a href="#" class="reply-to">
Upvotes: 1