PHP处理具有特定宽度和宽度的IMG。订购…

时间:2013-05-19 10:20:24

标签: php image width

我希望$_POST['textarea']内的IMG标记改为:

示例:

$_POST['textarea'] = <b>Some example Text</b><br> <img src="image_1.jpg" alt="bla bla" width="250" height="100" /> <u>some more text</u><br> <img src="image_2.jpg" alt="bla bla" width="50" height="20" />

采用以下格式:

$final = <b>Some example Text</b> <a class="fancybox" rel="gallery1" href="image_1.jpg" title="bla bla"><img src="image_1.jpg" alt="bla bla" width="250" height="100" /></a> </u>some more text</u> <img src="image_2.jpg" alt="bla bla" width="50" height="20" />
  1. 如何将class =“fancybox”仅添加到宽度大于50px的IMG标签中?
  2. 如何按照相应值处理所有IMG标记?
  3. 如何使用所有标签和文字&amp ;;打印Var $ _POST ['textarea']的全部内容。 Var $ final中更新的IMG标签?

1 个答案:

答案 0 :(得分:0)

在您的信息页中加入simple_html_dom.php。从http://en.sourceforge.jp/projects/sfnet_simplehtmldom/downloads/simplehtmldom/1.10/simplehtmldom_1_10.zip/

下载
 <?php
require_once('controller/simple_html_dom.php');
$str = 'Some example Text <img src="image_1.jpg" alt="bla bla" width="250" height="100" /> 
        some more text <img src="image_2.jpg" alt="bla bla" width="50" height="20" />';

$html = str_get_html($str);
$img = array();
foreach ($html->find('img') as $images) {
    if($images->getAttribute('width') > 50) {
        $img[] = '<a class="fancybox" rel="gallery1" href="'.$images->getAttribute('src').'" title="bla bla">'.$images.'</a>';
    } else {
        $img[] = $images;
    }
}

$full = '';

$i = 0;
foreach ($html->find('text') as $text) {
    $full .= $text.$img[$i];
    $i++;
}
echo $full;
?>