This is a small function to wrap a specific word in a text string with a html tag. So lets say I want to wrap the third word in
tags I would use below.
echo WrapWord('This is my line of text!', '3', 'b');
Above would output "This is
my line of text!" you will need the function for above to work otherwise it will most likely display a blank page, The function WrapWord(); is below.
function WrapWord
($text, $position, $tag) { $ex = explode(' ', $text); foreach ($ex as $key => $val) { if ($key == $position-1) { $val = "<${tag}>${val}</${tag}>"; } $word_string .= "${val} "; } return $word_string;}
some other examples below.
echo WrapWord('This is my line of text!', '4', 'i');
//Outputs "This is my <i>line</i> of text!"
echo WrapWord('This is my line of text!', '2', 'p');
//Outputs "This <p>is</p> my line of text!"
echo WrapWord('This is my line of text!', '1', 'span');
//Outputs "<span>This</span> is my line of text!"
and a full mark up of the php file.
<?phpfunction WrapWord
($text, $position, $tag) { $ex = explode(' ', $text); foreach ($ex as $key => $val) { if ($key == $position-1) { $val = "<${tag}>${val}</${tag}>"; } $word_string .= "${val} "; } return $word_string;}echo WrapWord
('This is my line of text!', '3', 'b');?>
Hope this comes in useful to some people.
~Jake
sunil0021
- Sunil Bishnoi Thanks for sharing jake :) Now a days i'm learning php for wordpress development, i will contact you in future if i need any complex language work :D [B]- [U]Sunil Bishnoi[/U][/B]
Are you sure you want to delete this post?