php批量给添加图片alt属性

 
更多

<?php
// require_once (dirname ( __FILE__ ) . "/include/common.inc.php");
// require_once ("/admin/inc/inc_archives_functions.php");
// $tables = array (
// '#@__addon7',
// '#@__addon8',
// '#@__addon9',
// '#@__addon10',
// '#@__addon11',
// '#@__addon12',
// '#@__addon13',
// '#@__addonarticle'
// );
// foreach ( $tables as $table ) {
// $dsql = "select aid,body from {$table};";
// $db->Execute ( 'me', $dsql );
// $Result = array ();
// while ( $arr = $db->GetArray () ) {
// // $body=preg_replace("/onclick=('|\")?(.*)\\1/sU",'',$arr ['body']);
// // $body=str_replace("style=\"cursor:pointer\" ","",$body);
// // $sql ="update hk_addonarticle set body='{$body}' where aid
// // ={$arr['aid']};";
// // $rs = $db->ExecuteNoneQuery2($sql);
// // $Result[$rs][]=$arr['aid'];
// // echo $arr['aid'] .'->'.$rs.'<hr/>';
// echo $arr ['aid'] . '<hr/>';
// ResizeContentImg ( $arr ['body'] );
// }
// }
$body = '<p>
    <img src="http://192.168.1.50:94/uploads/allimg/130807/1109243460-0.jpg">
</p>
<p>
    <img  alt=\'ssssss21\' src="http://192.168.1.50:94/uploads/allimg/130807/1109245452-1.jpg"  />
</p>
<p>
    <img src="http://192.168.1.50:94/uploads/allimg/130807/43941375844905.JPG" alt="ssssss3"  style="float:none;" title="DSC00454.JPG" border="0" hspace="0" vspace="0" /><br />
</p>';
// #alt="[^"]*"#i
// preg_match_all( "/alt\=[\"|\'](.*)[\"|\']/isU", $body, $img_array );取得所有alt内容
$tiltie = "3333";
echo autoImgAlt ( $body, $tiltie );
function autoImgAlt($body, $str) {
	preg_match_all ( '/(<img[^>]+src\s*=\s*"?([^>"\s]+)"?[^>]*>)/im', $body, $img_array );
	if (is_array ( $img_array ['1'] )) {
		$imgs = array ();
		foreach ( $img_array ['1'] as $img ) {
			$rs = preg_match ( "/alt\=[\"|\'](.*)[\"|\']/isU", $img, $alt );
			if ($rs) {
				$alt = $alt ['1'] . '_' . $str;
			} else {
				$alt = $str;
			}
			$img = str_replace ( "  ", " ", preg_replace ( "@ [\s]{0,}alt[\s]{0,}=[\"'\s]{0,}[\s\S]{0,}[\"'\s] @isU", " ", $img ) );
			$imgs [] = str_ireplace ( "<img ", "<img alt=\"{$alt}\" ", $img );
		}
	}
	$body = str_replace ( $img_array ['1'], $imgs, $body );
	return $body;
}
// $body = str_ireplace(array('alt=""','alt=\'\''),'',$body);

// $body= preg_replace("@ [\s]{0,}alt[\s]{0,}=[\"'\s]{0,}[\s\S]{0,}[\"'\s]
// @isU"," ",$body);

// $body= str_ireplace("<img " ,"<img alt='11111111' ",$body);

// echo $body . '<hr/>';
exit ();
?>

打赏

本文固定链接: https://www.cxy163.net/archives/935 | 绝缘体

该日志由 绝缘体.. 于 2013年08月07日 发表在 首页 分类下,
原创文章转载请注明: php批量给添加图片alt属性 | 绝缘体

报歉!评论已关闭.