 |
ใช้ str_replace() อยากให้ถ้าเจอเครื่องหมาย [[ ]] ให้ข้าไม่ต้องนำมารวมในการคำนวณ |
|
 |
|
|
 |
 |
|
คิดได้เท่านั้นครับ
Code (PHP)
$str = "the boy like to the play football [[ the very much the friend ]] thank the you";
preg_match('/\[\[.*\]\]/',$str,$matches);
$patterns = array('/the/','/\[\[.*\]\]/');
$replacements = array('a',$matches[0]);
$new_str = preg_replace($patterns, $replacements, $str);
echo $new_str;
|
 |
 |
 |
 |
Date :
2011-11-04 09:02:32 |
By :
kerb |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
กรณีมี [[]] ชุดเดียวนะครับ ถ้ามีหลายชุดต้องเขียนอีกแบบครับ
|
 |
 |
 |
 |
Date :
2011-11-04 09:04:36 |
By :
kerb |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากครับ แต่ยากจัง
|
 |
 |
 |
 |
Date :
2011-11-04 09:38:25 |
By :
tonnant |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใครพอจะทราบวิธีทำหลายๆชุดบ้างครับ
ผมงมมาพักใหญ่แล้วก็ยังไม่ได้
|
 |
 |
 |
 |
Date :
2011-11-04 14:27:17 |
By :
tonnant |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
$str = "the boy like to [[ the friend ]] the play football [[ the very much the friend ]] thank the you";
$reg = '/\[\[[^\]]*\]\]/';
preg_match_all($reg,$str,$matches);
$matches=$matches[0];
$strMatches=join('|',$matches);
$strMatches=str_replace('the','a',$strMatches);
$replacedMatches=explode('|',$strMatches);
$patterns = array_merge(array('the'),$replacedMatches);
$replacements = array_merge(array('a'),$matches);
$new_str =str_replace($patterns, $replacements, $str);
echo $new_str;
|
 |
 |
 |
 |
Date :
2011-11-04 16:54:47 |
By :
kerb |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่เข้าใจโค้ดตรงไหน สอบถามได้นะครับ
|
 |
 |
 |
 |
Date :
2011-11-04 16:56:14 |
By :
kerb |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อันนี้ Modify ใหม่ครับ สั้นกว่า ลดข้อผิดพลาดที่อาจเกิดขึ้นได้ตอน Join()
Code (PHP)
$reg = '/\[\[[^\]]*\]\]/';
$str = "the boy like to [[ the friend ]] the play football [[ the very much the friend ]] thank the you";
$strReplaced=str_replace('the','a',$str);
preg_match_all($reg,$str,$matches);
preg_match_all($reg,$strReplaced,$replacedMatches);
$patterns=$replacedMatches[0];
$replacements=$matches[0];
$new_str=str_replace($patterns, $replacements, $strReplaced);
echo $new_str;
|
 |
 |
 |
 |
Date :
2011-11-04 17:03:17 |
By :
kerb |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากครับ เดียวกลับไปเทส ไม่เข้าใจตรงไหน เดียวกลับมาถามใหม่นะครับ
|
 |
 |
 |
 |
Date :
2011-11-04 17:06:47 |
By :
tonnant |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 7 เขียนโดย : kerb เมื่อวันที่ 2011-11-04 17:03:17
รายละเอียดของการตอบ ::
ผมนำมาวนลูปในการ str_replace() ตามคำในฐานข้อมูล
ข้อมูล table spin
โค๊ดที่ผมนำไปแก้ไข
Code (PHP)
$str = "The quick black wolf jumped over the lazy dog";
$str = strtolower($str);
$reg = '/\[\[[^\]]*\]\]/';
$sql_array = "select spin_text from spin group by spin_text";
$result_array = mysql_query($sql_array);
$num_array = mysql_num_rows($result_array);
for($i=0;$i<$num_array;$i++){
$a = mysql_result($result_array, $i, 0);
$a3 = " " . $a . " ";
$sql2 = "select spin01 from spin where spin_text = '$a'";
$result2 = mysql_query($sql2);
$num2 = mysql_num_rows($result2);
$a2 = " [[" ;
for($j = 0;$j<$num2;$j++){
$a2 .= mysql_result($result2, $j, 0). "|";
}
$a2 .= "$a]] ";
//$a2 = " {{quick|fast}} ";
$new_detail = str_replace($a3,$a2,$str);
preg_match_all($reg,$str,$matches);
preg_match_all($reg,$new_detail,$replacedMatches);
$patterns=$replacedMatches[0];
$replacements=$matches[0];
$str = str_replace($patterns, $replacements, $new_detail);
}
จากอินพุท
The quick black wolf jumped over the lazy dog
ผลลัพธ์ที่ได้
the black over dog
แต่ผลลองเปลี่ยน
Code (PHP)
$a2 = " {{" ; //ของเดิม $a2 = " [[" ;
for($j = 0;$j<$num2;$j++){
$a2 .= mysql_result($result2, $j, 0). "|";
}
$a2 .= "$a}} "; //ของเดิม $a2 .= "$a]] ";
ผลลัพธ์ที่ได้
the {{fast|speedy|quick}} black {{fox|wolf}} {{bounded|hopped|skipped|jumped}} over {{a|the}} {{tired|lazy}} dog
แค่เอาผลลัพธ์นี้มา รีเพลซ เครื่องหมายกลับมาเป็น [[ ]] เหมือนเดิม
แต่ผมไม่แน่ใจว่ามันจะครอบคลุมถูกต้องจริงๆมั้ย
|
ประวัติการแก้ไข 2011-11-05 00:43:04
 |
 |
 |
 |
Date :
2011-11-05 00:40:52 |
By :
tonnant |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
สรุปว่าจะทำอะไรครับ งง ไม่เห็นได้ใช้ function ที่เขียนให้เลยครับ
|
 |
 |
 |
 |
Date :
2011-11-05 21:39:57 |
By :
kerb |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|