PDA

View Full Version : پاک کردن استایل متون یک پاراگراف و ذخیره ی متن تنها، در بانک اطلاعات



ali abedian
شنبه 28 تیر 1393, 22:58 عصر
سلام دوستان و اساتید گرامی
من یه وبلاگی دارم که کلی اطلاعات داخلش هست و روی بلاگفاست..
نشستم یه سی ام اس براش نوشتم ومیخوام کل مطالب رو که چندین ساله زحمتشو کشیدم به سایت اختصاصی انتقال بدم...
مشکلی اینجاست:
شما فرض کن که توی یک پست از بلاگفا، من برای پارگرافهای مختلف استایلهای مختلفی رو قرار دادم (مثلا bold یا رنگ یا سایز فونتها) من وقتی اینها رو کپی میکنم و در cms سایت جدیدم در بخش افزودن مطالب past میکنم، مطلب بلاگفا با تمام استایلهای تعریف شده به سایت جدیدم منتقل میشه و من نمیخوام اینطور بشه.
یه گزینه ای هست توی همین ادیتور این انجمن (و خیلی جاهای دیگه) هم هست که کار remove format رو انجام میده، خواستم ببینم که چطور میتونم برای باکس متن ورودی در cms ای که طراحی کردم، میشه این امکان رو قرار داد... اینو هم توضیح بدم که من نمیخوام از پلاگینهای آماده ی ایدیتورهای مشهور در برنامه م استفاده کنم.
البته یه نکته ای رو هم بگم. وقتی من از بلاگفا کپی میکنم، میخوام تمام لینکها حفظ بشه و هم اینکه عکسهایی که در مطلب قرار داره هم حفظ بشه و لینک ویدئوهای آپارت هم همینطور.

.....

البته میخوام یه طوری کنم که همه ی لینکهایی که روی تصاویر هستند نیز حذف شوند

ali abedian
یک شنبه 29 تیر 1393, 10:32 صبح
چقدر این انجمن بی روحه خیلی دیر جواب میدن

ali abedian
چهارشنبه 01 مرداد 1393, 15:56 عصر
جواب نبود؟

arenaw
چهارشنبه 01 مرداد 1393, 16:06 عصر
مثلا این متن اینجوری ذخیره شده:




<u>مثلا</u> این <strong>متن</strong> اینجوری <em>ذخیره</em> شده:


کاری که شما باید بکنی، اینه که متن پست هاتونو رو از تابع strip_tags رد بکنید. این تابع میاد تمام تگ های اچ تی ام ال رو از متن حذف میکنه.

vahidqara
چهارشنبه 01 مرداد 1393, 16:24 عصر
جواب نبود؟

درود .. عزیزم هیچ کسی اینجا تعهدی نسبت به جواب دادن سوال شما و هر کسی دیگه رو نداره دوستان لطف می کنند و جواب میدن .. همین :لبخندساده:

thacker
چهارشنبه 01 مرداد 1393, 16:35 عصر
با استفاده از کد زیر تمام تگ ها بجز تگ <a> و <img> حذف میشوند :


strip_tags($post, '<a><img>')

ali abedian
دوشنبه 06 مرداد 1393, 23:35 عصر
درود .. عزیزم هیچ کسی اینجا تعهدی نسبت به جواب دادن سوال شما و هر کسی دیگه رو نداره دوستان لطف می کنند و جواب میدن .. همین :لبخندساده:

خیلی تعجب میکنم از آدمهایی که بیخود و بی جهت، جوابهای نامربوط و پوچ میدن. آخه شما بگو من کجای نوشته هام آوردم که اعضای این انجمن نسبت به پرسشهای من و دیگران تعهد به پاسخ دادن دارن؟
بازم میگم این انجمن نسبت به برخی انجمنها خیلی بی روح هست. در اینکه دوستان لطف میکنن و جواب میدن شکی نیست ولی یادمون باشه که هیچ کسی ، تاکید میکنم هیچ کسی پیدا نمیکنی (از زمان حضرت آدم تا الان)، که کاری کنه که سود یا ضررش به خودش برنگرده. این دوستانی که لطف میکنن ، مطمئناً بیشتر از همه به خودشون لطف میکنن.
من هم به مقدار پاسخگویی به سوالاتم در این انجمن از اونهایی که پاسخ دادن و یا حتی توجهی نشون دادن سپاسگزارم.

ali abedian
سه شنبه 07 مرداد 1393, 23:53 عصر
ممنون دوستان مشکلم حل شد با راهنمایی های با ارزش شما..
سوال بعدی ای که پیش میاد اینه که چطور میشه استایل داده شده به مثلاً تگ <p> رو حذف کرد ولی خود تگ <p> حذف نشه؟!!!
ممنون میشم در این مورد هم راهنمایی بدید
مثلا میخوام این تکه کد:

<p style="color: rgb(133, 134, 134); font-family: Tahoma, Geneva, sans-serif; font-size: 13px; text-align: center; background-color: rgb(235, 237, 238);">

متن پاراگراف

</p>
ققط استایلش حذف بشه

ali abedian
سه شنبه 07 مرداد 1393, 23:55 عصر
مثلا این متن اینجوری ذخیره شده:




<u>مثلا</u> این <strong>متن</strong> اینجوری <em>ذخیره</em> شده:



کاری که شما باید بکنی، اینه که متن پست هاتونو رو از تابع strip_tags رد بکنید. این تابع میاد تمام تگ های اچ تی ام ال رو از متن حذف میکنه.

من در تیکت بالایی هم گفتم، من میخوام که در این نمونه ای که خودتون مثال زدید، فقط بخش استایلش حذف بشه و بقیه قسمتهاش بمونه

arta.nasiri
چهارشنبه 08 مرداد 1393, 00:51 صبح
http://php.net/manual/en/function.preg-replace.php


<?php
$data = '<p style="color: rgb(133, 134, 134); font-family: Tahoma, Geneva, sans-serif; font-size: 13px; text-align: center; background-color: rgb(235, 237, 238);">';
$data = preg_replace('#style=\".*\"#', '', $data);
?>

ali abedian
چهارشنبه 08 مرداد 1393, 10:22 صبح
از همه دوستان سپاسگزارم...

علت سوالم اینه که میخوام از مطالب رو از وبلاگ ۵ ساله ی بلاگفای خودم به سایت جدیدی که cms ش رو برای خودم طراحی کردم ، انتقال بدم.
من نمیخوام پستهای بلاگفا با کدهای اضافی درون بانک اطلاعاتی سایتم ذخیره بشه و حجم بانک رو بالا ببره
به عنوان یک مثال دقیق میخوام اتفاق زیر بیافته:

میخوام این کد (که کد پستهای بلاگفای وبلاگم هست)

<br><p style="color: rgb(133, 134, 134); font-family: Tahoma, Geneva, sans-serif; font-size: 13px; text-align: center; background-color: rgb(235, 237, 238);"><img src="http://cld.persiangig.com/preview/Iym4bzRRpq/banner-ramazan-1.jpg" alt="" width="400" height="527" style="border: none;"></p><p style="color: rgb(133, 134, 134); font-family: Tahoma, Geneva, sans-serif; font-size: 13px; text-align: center; background-color: rgb(235, 237, 238);">بسم الله الرحمن الرحیم</p><p style="color: rgb(133, 134, 134); font-family: Tahoma, Geneva, sans-serif; font-size: 13px; text-align: center; background-color: rgb(235, 237, 238);">&nbsp;</p><p style="color: rgb(133, 134, 134); font-family: Tahoma, Geneva, sans-serif; font-size: 13px; text-align: justify; background-color: rgb(235, 237, 238);">اللهمّ&nbsp;ارْزُقنی فیهِ الذّهْنَ والتّنَبیهَ وباعِدْنی فیهِ من السّفاهة والتّمْویهِ واجْعَل لی نصیباً مِنْ کلّ خَیْرٍ تُنَزّلُ فیهِ بِجودِکَ یا أجْوَدَ الأجْوَدینَ.</p><p style="color: rgb(133, 134, 134); font-family: Tahoma, Geneva, sans-serif; font-size: 13px; text-align: justify; background-color: rgb(235, 237, 238);">پروردگارا در این روز عزیز نعمت خودآگاهی و بیداری ذهن را روزیم گردان و از جهل و نادانی و خطا بدورم دار و از هرآنچه خیری که در این ماه نازل می گردانی برایم بهره و نصیبی قرار ده و به حق روز و کرمت باران بخششت را بر من ببار. ای بخشنده ترین بخشندگان.</p><br style="color: rgb(133, 134, 134); font-family: Tahoma, Geneva, sans-serif; font-size: 13px; background-color: rgb(235, 237, 238);">

به کد زیر تبدیل بشه و در بانک ذخیره بشه


<br><p><img src="http://cld.persiangig.com/preview/Iym4bzRRpq/banner-ramazan-1.jpg" width="400" height="527"></p><p>بسم الله الرحمن الرحیم</p><p>اللهمّ ارْزُقنی فیهِ الذّهْنَ والتّنَبیهَ وباعِدْنی فیهِ من السّفاهة والتّمْویهِ واجْعَل لی نصیباً مِنْ کلّ خَیْرٍ تُنَزّلُ فیهِ بِجودِکَ یا أجْوَدَ الأجْوَدینَ.</p><p>پروردگارا در این روز عزیز نعمت خودآگاهی و بیداری ذهن را روزیم گردان و از جهل و نادانی و خطا بدورم دار و از هرآنچه خیری که در این ماه نازل می گردانی برایم بهره و نصیبی قرار ده و به حق روز و کرمت باران بخششت را بر من ببار. ای بخشنده ترین بخشندگان.</p>


راهنمایی دوستان خیلی مفید بود ولی خب من نتونستم اونطوری که میخوام ازش استفاده کنم و استایلهای درون کدهای اولیه رو ازش بردارم و کلا چیزهای اضافی رو ازش حذف کنم حتی این کد رو که خالی هم هست

alt=""
یا کد زیر رو که ضرورت نداره باشه چون من به طور کلی در استایل سایت تعریف کردم که تصاویر حاشیه نداشته باشن

style="border: none;"

aalmair
چهارشنبه 08 مرداد 1393, 13:27 عصر
<?php
header("Content-Type: text/html; charset=utf-8");

class html_parser{

private $article='';
private $size_article='';

private $code_obj=array();
private $special_tag=array('style','script','title','texta rea','nobody','canvas') ;



/////////////////////////////////////////


public $key_is_start_tag='is';
public $key_is_extra_value='ix';
public $key_is_end_tag='ie';
public $key_is_content='ic';
public $key_value='v';
public $key_is_attribute='ia';
public $key_attribute='av';

/////////////////////////////



function __construct(){













}
private function find_sub_case($sub='',$start=0){
$str=$this->article;
$size_sub=strlen($sub);
$size_str=$this->size_article;
for($i=$start;$i<$size_str;$i++){

$j=0;
$k=$i;
while($j<$size_sub&&strcasecmp($sub[$j],$str[$k])==0){
$j++;
$k++;
}
if($j==$size_sub){
return $k;
}

}
return -1;
}
private function atrributes_str($start_index=0,&$attributes_str_res=''){

//start index must be ' ' or >'>'
$div_sign_end_tag=0;
$attributes_str_res='';
$attributes_str='';
$i=$start_index;
$end_of_attribute=$i;
$article=$this->article;
$size_article=$this->size_article;

$attributes=array();

$end_of_tag=0;

if($i>=$size_article){
return $i;
}



if(!(ord($article[$i])<=32||$article[$i]=='>')){
return $i;
}



while($i<$size_article){
if($article[$i]==">"){
//$i++;
$attributes_str_res=$attributes_str;
return $i;

break;
}elseif($i+1<=$size_article&&$article[$i]=='/'&&$article[$i+1]=='>'){

$i++;
$attributes_str_res=$attributes_str.' /';
return $i;
}

if($article[$i]=='=' &&ord($article[$i-1])>32){
$j=$i-1;
$k=$j;

while($j>=0){
if((($j-1)>=0&&ord($article[$j-1])<=32)||($j-1)<=$end_of_attribute){
break;
}
$j--;
}

if(isset($attribute_name)){
unset($attribute_name);
}

if($j<$k){

$attribute_name=substr($article,$j,$k-$j+1);
}elseif($j==$k){
$attribute_name=$article[$j];
}






$i++;
while($i<$size_article&&ord($article[$i])<=32){
$i++;
}





$attribute_value='';

if($article[$i]=="'"){
$i++;
while($i<$size_article){
if($article[$i]=="'"){
$end_of_attribute=$i;
break;
}
$attribute_value.=$article[$i];
$i++;
}

}elseif($article[$i]=='"'){
$i++;
while($i<$size_article){
if($article[$i]=='"'){
$end_of_attribute=$i;
break;
}
$attribute_value.=$article[$i];
$i++;
}

}else{

while($i<$size_article&&ord($article[$i])<=32){

$i++;

}

while($i<$size_article&&ord($article[$i])>32){

if($article[$i]=='>'){

$end_of_tag=1;
break;
}elseif($i+1<=$size_article&&$article[$i]=='/'&&$article[$i+1]=='>'){
$i++;
$div_sign_end_tag=1;
$end_of_tag=1;
break;
}else{
$attribute_value.=$article[$i];
$i++;
}



}

}

if(isset($attribute_name)&&$attribute_name){
$attribute_name=strtolower($attribute_name);


$attribute_name_exist=0;
for($t=0;$t<count($attributes);$t+=2){
if($attributes[$t]==$attribute_name){
$attribute_name_exist=1;
break;
}
}

if($attribute_name_exist==0){
$attributes[]=$attribute_name;
$attributes[]=$attribute_value;

$disallowd_attri=0;



$attributes_str.=' ';
for($c=0;$c<strlen($attribute_name);$c++){

$attributes_str.=$attribute_name[$c];

}
$attributes_str.='="'.$attribute_value.'"';


}
unset($attribute_name);

}

}
if($end_of_tag){
break;
}

$i++;

}
if($div_sign_end_tag){
$attributes_str.=' /';
}
$attributes_str_res=$attributes_str;
return $i;

}
private function attribute_parse($start_index=0,&$attributes_arr_res=array()){

//start index must be ' ' or >'>'


$attributes_arr_res=array();
$i=$start_index;
$end_of_attribute=$i;
$article=$this->article;
$size_article=$this->size_article;

$temp_attributes=array();
$attributes=array();

$end_of_tag=0;


if($i>=$size_article){
return $i;
}


if(!(ord($article[$i])<=32||$article[$i]=='>')){
return $i;
}



while($i<$size_article){
if($article[$i]==">"){
//$i++;
$attributes_arr_res=$attributes;
return $i;

break;
}

if($article[$i]=='=' &&ord($article[$i-1])>32){
$j=$i-1;
$k=$j;

while($j>=0){
if((($j-1)>=0&&ord($article[$j-1])<=32)||($j-1)<=$end_of_attribute){
break;
}
$j--;
}




if(isset($attribute_name)){
unset($attribute_name);
}




if($j<$k){

$attribute_name=substr($article,$j,$k-$j+1);
}elseif($j==$k){
$attribute_name=$article[$j];
}






$i++;
while($i<$size_article&&ord($article[$i])<=32){
$i++;
}





$attribute_value='';

if($article[$i]=="'"){
$i++;
while($i<$size_article){
if($article[$i]=="'"){
$end_of_attribute=$i;
break;
}
$attribute_value.=$article[$i];
$i++;
}

}elseif($article[$i]=='"'){
$i++;
while($i<$size_article){
if($article[$i]=='"'){
$end_of_attribute=$i;
break;
}
$attribute_value.=$article[$i];
$i++;
}

}else{

while($i<$size_article&&ord($article[$i])<=32){

$i++;

}

while($i<$size_article&&ord($article[$i])>32){

if($article[$i]=='>'){
$end_of_tag=1;
break;
}
$attribute_value.=$article[$i];
$i++;
}

}

if(isset($attribute_name)&&$attribute_name){
$attribute_name=strtolower($attribute_name);


$attribute_name_exist=0;
for($t=0;$t<count($temp_attributes);$t+=2){
if($temp_attributes[$t]==$attribute_name){
$attribute_name_exist=1;
break;
}
}

if($attribute_name_exist==0){
$temp_attributes[]=$attribute_name;
$temp_attributes[]=$attribute_value;
$attributes[strtolower($attribute_name)]=$attribute_value;
}
unset($attribute_name);

}

}
if($end_of_tag){
break;
}

$i++;

}
$attributes_arr_res=$attributes;
return $i;

}
public function parse($article=''){

$this->article=$article;
$this->size_article=strlen($article);







$i=0;

$size_article=$this->size_article;
$temp_res='';
$res_arr=array();


while($i<$size_article){

if($i+1<$size_article&&$article[$i]=='<'&&ctype_alpha($article[$i+1])){

if(strlen($temp_res)){
$res_arr[]=array($this->key_is_content=>1,$this->key_value=>$temp_res);
$temp_res='';
}





$f_index_of_start_tag=$i+1;
$tag_name='';
$attribute_exist=1;
$i++;

while($i<$size_article&&ord($article[$i])>32){
if($article[$i]=='>'){
$attribute_exist=0;
$i++;
break;
}
$tag_name.=$article[$i];

$i++;

}
$tag_name=strtolower($tag_name);



$special_tag=0;
for($c=0,$count_special_tag=count($this->special_tag);$c<$count_special_tag;$c++){
if(strcasecmp($tag_name,$this->special_tag[$c])==0){
$special_tag=1;
$res='';
break;

}

}







if($special_tag){

$attributes_arr=array();

if($attribute_exist){

$i=$this->attribute_parse($i,$attributes_arr);

$i++;

}

$l_index_of_start_tag=$i;



$latest_find_index=$this->find_sub_case('</'.$tag_name,$l_index_of_start_tag);

if($latest_find_index==-1){


$i=$size_article;
$latest_find_index=$size_article;
$tag_value=(substr($article,$l_index_of_start_tag, $latest_find_index-$l_index_of_start_tag-1));

} else{
$i=$latest_find_index;
$tag_value=(substr($article,$l_index_of_start_tag, $latest_find_index-$l_index_of_start_tag-strlen("</$tag_name")));
$i=$this->attribute_parse($i,$temp_arr);
$i++;

}


if($attributes_arr){
// $res_arr[]=array('type'=>'start','value'=>strtolower($tag_name),'attributes'=>$attributes_arr);
$res_arr[]=array($this->key_is_start_tag=>1,$this->key_value=>strtolower($tag_name),$this->key_is_attribute=>1,$this->key_attribute=>$attributes_arr);
}else{
// $res_arr[]=array('type'=>'start','value'=>strtolower($tag_name));
$res_arr[]=array($this->key_is_start_tag=>1,$this->key_value=>strtolower($tag_name));
}

// $res_arr[]=array('type'=>'etext','value'=>$tag_value);
$res_arr[]=array($this->key_is_extra_value=>1,$this->key_value=>$tag_value);
//$res_arr[]=array('type'=>'end','value'=>strtolower($tag_name));
$res_arr[]=array($this->key_is_end_tag=>1,$this->key_value=>strtolower($tag_name));




}else{
if($attribute_exist){
$attributes_arr=array();
$i=$this->attribute_parse($i,$attributes_arr);

$i++;

// $res_arr[]=array('type'=>'start','value'=>strtolower($tag_name),'attributes'=>$attributes_arr);
$res_arr[]=array($this->key_is_start_tag=>1,$this->key_value=>strtolower($tag_name),$this->key_is_attribute=>1,$this->key_attribute=>$attributes_arr);
// $temp_res.='<'.$tag_name.$attributes_str.'>';
}else{

// $temp_res.='<'.$tag_name.'>';
$res_arr[]=array($this->key_is_start_tag=>1,$this->key_value=>strtolower($tag_name));


}
}

}
elseif($i+2<$size_article&&$article[$i]=='<'&&$article[$i+1]=='/'){

if(strlen($temp_res)){
// $res_arr[]=array('type'=>'text','value'=>$temp_res);
$res_arr[]=array($this->key_is_content=>1,$this->key_value=>$temp_res);
$temp_res='';
}


if(ctype_alpha($article[$i+2])) {


$f_index_of_start_tag=$i+2;
$end_tag_name='';
$attribute_exist=1;
$i+=2;

while($i<$size_article&&ord($article[$i])>32){
if($article[$i]=='>'){
$attribute_exist=0;
$i++;
break;
}
$end_tag_name.=$article[$i];
$i++;

}




if($attribute_exist){

$attributes_str='';
$i=$this->atrributes_str($i,$attributes_str);
$i++;
if(1){
// $temp_res.='</'.$end_tag_name.''.'>';
// $res_arr[]=array('type'=>'end','value'=>strtolower($end_tag_name));
$res_arr[]=array($this->key_is_end_tag=>1,$this->key_value=>strtolower($end_tag_name));
}
}else{
if(1){
//$temp_res.='</'.$end_tag_name.'>';
$res_arr[]=array($this->key_is_end_tag=>1,$this->key_value=>strtolower($end_tag_name));
}
}




} else{
while($i<$size_article){

if($article[$i]=='>'){
$temp_res.=$article[$i];
$i++;
break;
}
$temp_res.=$article[$i];
$i++;
}


if(strlen($temp_res)){
// $res_arr[]=array('type'=>'etext','value'=>$temp_res);
$res_arr[]=array($this->key_is_extra_value=>1,$this->key_value=>$temp_res);
$temp_res='';
}

}




}
elseif($i+1<$size_article&&$article[$i]=='<'&&$article[$i+1]=='!'){

if(strlen($temp_res)){
// $res_arr[]=array('type'=>'text','value'=>$temp_res);
$res_arr[]=array($this->key_is_content=>1,$this->key_value=>$temp_res);
$temp_res='';
}



while($i<$size_article){

if($article[$i]=='>'){
$temp_res.=$article[$i];
$i++;
break;
}
$temp_res.=$article[$i];
$i++;
}

if(strlen($temp_res)){
// $res_arr[]=array('type'=>'etext','value'=>$temp_res);
$res_arr[]=array($this->key_is_extra_value=>1,$this->key_value=>$temp_res);
$temp_res='';
}


}
else{

$temp_res.=$article[$i++];






}

// $i++;
}


if(strlen($temp_res)){
// $res_arr[]=array('type'=>'text','value'=>$temp_res);
$res_arr[]=array($this->key_is_content=>1,$this->key_value=>$temp_res);
$temp_res='';
}

$this->code_obj= $res_arr;
return 1;

}
public function get_code_obj(){

return $this->code_obj;
}




} // end class


$html_parser=new html_parser();

$html_parser->parse('<div style="text-align: left;"><div style="margin-right:40px"><u>مثلا</u> این <strong>متن</strong> اینجوری <em>ذخیره</em> شده:</div></div>') ;

$html_code_obj=($html_parser->get_code_obj()) ;
$size_html_code_obj=count($html_code_obj);
$html_final="";


for($i=0;$i<$size_html_code_obj;$i++)
{
$currect_node=$html_code_obj[$i];



if(isset($currect_node[$html_parser->key_is_start_tag]))
{
$node_attributes=array();
$tag_name=$currect_node[$html_parser->key_value];
$html_final.="<".$currect_node[$html_parser->key_value];

if(isset($currect_node[$html_parser->key_is_attribute]))
{
$node_attributes=$currect_node[$html_parser->key_attribute];

foreach($currect_node[$html_parser->key_attribute] as $attribute_key=>$attribute_value)
{

if($attribute_key=='style')
{
continue;
}
$html_final .=" " . $attribute_key .'=' ;



if(strstr($attribute_value,"'"))
{
$html_final .= '"'. $attribute_value .'"' ;
}elseif(strstr($attribute_value,'"'))
{
$html_final .= "'". $attribute_value ."'" ;
}else
{
$html_final .= '"'. $attribute_value .'"' ;
}
}
$html_final .= ">";
}else
{
$html_final .= ">";
}


}
elseif(isset($currect_node[$html_parser->key_is_content]))
{

$html_final .= $currect_node[$html_parser->key_value];

}
elseif(isset($currect_node[$html_parser->key_is_end_tag]))
{

$html_final .="</". $currect_node[$html_parser->key_value]."/>";

}

}



var_dump($html_final) ;