php string operation
I have stucked into a typical php string program. I have a data string like
$str="{heading}
{youtube}4vfOPm6wfyI{/youtube}
{/heading}
<p>Sed ut perspiciatis unde omnis iste n开发者_StackOverflow中文版atus error sit voluptatem accusantium doloremque laudantium</p>";
now i want the 4vfOPm6wfyI , the id of the link. How it will be done?
Thanks
Use explode to find the key. This will extract the key for you:
$arr = explode("{youtube}", $str);
$arr = explode("{/youtube}", $arr[1]);
$key = $arr[0];
print $key;
Just do a regular expression search/replace of \{youtube\}(.*?)\{\/youtube\}
with the proper embed line:
<object width="640" height="385">
<param name="movie" value="http://www.youtube.com/v/$1"></param>
<param name="allowFullScreen" value="true"></param>
<param name="allowscriptaccess" value="always"></param>
<embed src="http://www.youtube.com/v/$1"
type="application/x-shockwave-flash"
allowscriptaccess="always"
allowfullscreen="true"
width="640"
height="385"></embed>
</object>
EDIT: If you just want to capture what's in between the {youtube} and {/youtube}, do something like this:
if (ereg ("\{youtube\}(.*?)\{\/youtube\}", $str, $regs)) {
print "<p>The youtube link is: http://www.youtube.com/v/$regs[1]</p>";
}
精彩评论