English 中文(简体)
如何把某些标签及其内容代之于在营地的习俗文本?
原标题:how do i replace some tags and their content with custom text in php?

i 采用<代码>preg_match_all,在座标上找到某些标记:

$str = " 
Line 1: This is a string 
Line 2: [img]http://placehold.it/350x150[/img] Should not be [youtube]SDeWJqKx3Y0[/youtube] included."; 

preg_match_all("~[img](.+?)[/img]~i", $str, $img);  
preg_match_all("~[youtube](.+?)[/youtube]~i", $str, $youtube);

foreach ($img[1] as $key => $value) {
    echo  <img src=" .$value. " "/> ;
}

foreach ($youtube[1] as $key => $value) {
    echo  <iframe width="420" height="315" src="http://www.youtube.com/embed/ .$value. " frameborder="0" allowfullscreen> </iframe> ;
}

这将完全恢复正与正确价值观相呼应的东西。

但实际想要的是将整条插法与<代码>[img]和[youline]等标号重新编号,代之以各声明的数值:

Line 1: This is a string 
    Line 2: <img src="http://placehold.it/350x150" "/> Should not be <iframe width="420" height="315" src="http://www.youtube.com/embed/SDeWJqKx3Y0" frameborder="0" allowfullscreen> </iframe> included.

i m not looking for a 3rd party alternative, just plain php functions.

i mink of using preg_match and some case and switch statements, but i don tred

想法?

最佳回答

您可使用preg_replace

与此类似。

$pattern = Array();
$pattern[0] = "~[img](.+?)[/img]~i";
$pattern[1] = "~[youtube](.+?)[/youtube]~i";

$replacement = Array();
$replacement[0] =  <img src="${1}" "/> ;
$replacement[1] =   <iframe width="420" height="315" src="http://www.youtube.com/embed/${1}" frameborder="0" allowfullscreen> </iframe> ;

$stringToReturn = preg_replace($pattern, $replacement, $str);
问题回答

暂无回答




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签