php - Convert *text* into <span>text</span> with function -


this question has answer here:

how can take string this: *up to* $1,000

and turn this: <span>up to</span> $1,000

the stars can anywhere in string , there can multiple sets of stars. each set should replaced span's.

e.g.

text *test* here = text <span>test</span> here

text here *test* right *now* = text here <span>test</span> right <span>now</span>

i need able pass value function , receive formatted string in return. ahead of time.

simple regex can this:

function replace_star($str) {     return preg_replace('~\*([^*]*)\*~ms', '<span>\1</span>', $str); }  echo replace_star('*up to* $1,000') . "\n"; echo replace_star('text here *test* right *now*'); 

output:

<span>up to</span> $1,000 text here <span>test</span> right <span>now</span> 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -