การแทนค่าจากตำแหน่ง 2 ไป ตำแหน่ง 1 ทำอย่าง

เริ่มโดย chinakron, 06 มิถุนายน 2008, 01:03:24

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

chinakron

อ้างถึง<a href=userdetails.php?id=29209><b>bit99bit</b></a>
<a href=userdetails.php?id=23719><b>BITBANGKOK</b></a>
<a href=userdetails.php?id=24222><b>bitchy</b></a>

จากข้อมูลข้างบน จะใช้คำสั่งอย่างไง ให้ข้อความ ใน <b>?? ?? ?? ?</b> มาแทนค่าใน หลัง ?id=?? ??

ใช้คำสั่ง preg_match() แต่มันเอาค่าแรกมาแทนหมดเลยคือค่า bit99bit เจ็บจายยย ไปต่อม่ายได้แย้วว

ผู้รู้ช่วยถ่ายทอดวิชาทีงับ.....

payu


แบบนี้ป่ะ ...


$t = <<<EOT
<a href=userdetails.php?id=29209><b>bit99bit</b></a>
<a href=userdetails.php?id=23719><b>BITBANGKOK</b></a>
<a href=userdetails.php?id=24222><b>bitchy</b></a>

EOT;

$m = preg_replace('#(<a href=userdetails\.php\?id=)([0-9]*)(><b>)(.*)(</b></a>)#', '\\1\\4\\3\\4\\5', $t);

echo $m;
[direct=http://www.facebook.com/iipayu]payu on facebook[/direct]

thenetxx

Develop site but can't develop life
ASIA

chinakron

อ้างถึงจาก: payu ใน 06 มิถุนายน 2008, 09:50:13

แบบนี้ป่ะ ...


$t = <<<EOT
<a href=userdetails.php?id=29209><b>bit99bit</b></a>
<a href=userdetails.php?id=23719><b>BITBANGKOK</b></a>
<a href=userdetails.php?id=24222><b>bitchy</b></a>

EOT;

$m = preg_replace('#(<a href=userdetails\.php\?id=)([0-9]*)(><b>)(.*)(</b></a>)#', '\\1\\4\\3\\4\\5', $t);

echo $m;


ใช้ครับ แต่ '\\1\\4\\3\\4\\5' มันคืออะไรงับ ไม่เข้าจายย

payu


\\x = x คือ ชุดที่ .. ของ pattern ที่ match (match โดยวงเล็บ)
[direct=http://www.facebook.com/iipayu]payu on facebook[/direct]

chinakron

อ้างถึงจาก: payu ใน 07 มิถุนายน 2008, 01:21:02

\\x = x คือ ชุดที่ .. ของ pattern ที่ match (match โดยวงเล็บ)


ขอบคุณมากครับ แต่ถ้าในข้อความมีอักษรพวก ?%$#@& จะตัดออกยังไงครับ ผมใช้ function  ประมาณนี้งับ

อ้างถึงfunction preUrl($output) {
    if(php_version < 5) {   
      $output = html_entity_decode($output,ENT_QUOTES);
    } else {
      $output = html_entity_decode($output,ENT_QUOTES,'UTF-8');
    }
    $output = str_replace("&", " and ", $output);
    $output = str_replace(";", " ", $output);    
    $output = str_replace(":", " ", $output);
    $output = str_replace("...", " ", $output);    
    $output = str_replace(".", " ", $output);    
    $output = str_replace("?", " ", $output);    
    $output = str_replace("=", " ", $output);    
    $output = str_replace("+", " ", $output);    
    $output = str_replace("$", " ", $output);         
    $output = str_replace("!", " ", $output);    
    $output = str_replace(">>", " ", $output);    
    $output = str_replace(">", " ", $output);    
    $output = str_replace("<<", " ", $output);    
    $output = str_replace("<", " ", $output);    
    $output = str_replace("*", " ", $output);    
    $output = str_replace(")", " ", $output);    
    $output = str_replace("(", " ", $output);
    $output = str_replace("[", " ", $output);
    $output = str_replace("]", " ", $output);
    $output = str_replace("^", " ", $output);


    return str_replace("  ", " ", trim($output));
  } 

แล้วเวลาใช้งาน ใช้ preg_replace หรือ preg_match_all ดีกว่ากันครับ เพราะบางที มันแปลงให้ไม่หมดครับ

payu


แทนค่าเป็นชุด 1-1 ลองดู function นี้ครับ
http://th2.php.net/strtr

ส่วนการแทนตัวอักษรพิเศษ ลองนี่ครับ
preg_replace("#[;:.?=+$!><*)([\]^]#", " ", $output);

\] ต้อง cast ด้วย \ เพื่อไม่ให้มันมองว่าเป็นตัวจบของ [
อ่านเพิ่มเติมครับ ..
http://th2.php.net/preg_replace
[direct=http://www.facebook.com/iipayu]payu on facebook[/direct]

chinakron

อ้างถึง<a href=userdetails.php?id=29209><b>(Ñ)it99bit(Ñ)</b></a>
<a href=userdetails.php?id=23719><b><->NGKOK<-></b></a>
<a href=userdetails.php?id=24222><b>bi@chy</b></a>


จากอันเดิม จะใช้คำสั่งอย่างไง ให้ข้อความ ใน <b>?? ?? ?? ?</b> มาแทนค่าใน หลัง ?id=?? ??
แต่ให้มีการตัดอักษรพิเศษ

ผมใช้คำสั่งแบบนี้ครับ ที่ลอง แต่ทำงานได้รอบ เดี่ยว หรือเปลี่ยนค่าไม่ตรงตามที่กำหนด
อ้างถึง
if(preg_match_all("|<a\\shref=userdetails.php?id=([^>]*)><b>([^<]*)</b></a>|U",$myspace ,$matches,PREG_SET_ORDER)) {
foreach($matches as $key => $value) {
$pattern = '#userdetails.php?id=#';
$replace = 'userdetails.php?id='.makeurl($value[2]).'';
$body = preg_replace($pattern, $replace, $myspace);     
}             
}

function makeurl() ใช้ตัดอักษรพิเศษครับ

ปัญหาสำคัญค่ามันจำซ้ำกันอ่ะ
เปลี่ยนไปใช้แบบนี้ ค่าที่ได้ไม่ตรงกันอีก ไม่รู้เพราะอะไร
อ้างถึง$body = preg_replace($pattern, $replace, $myspace, 1 ); 



EThaiZone

function preUrl($output) {
if(php_version < 5) {   
$output = html_entity_decode($output,ENT_QUOTES);
} else {
$output = html_entity_decode($output,ENT_QUOTES,'UTF-8');
}
$output = str_replace("&", " and ", $output);
$out = array(";", ":", ".", "?", "=", "+", "$", "!", ">" , "<", "*", ")", "(", "[", "]", "^");
$output = str_replace($out, " ", $output);

return preg_replace("# +#", " ", trim($output));
}

$t = <<<EOT
<a href=userdetails.php?id=29209><b>(Ñ)it99bit(Ñ)</b></a>
<a href=userdetails.php?id=23719><b><->NGKOK<-></b></a>
<a href=userdetails.php?id=24222><b>bi@chy</b></a>

EOT;

preg_match_all('#<b>(.*)</b>#i', $t, $match);

foreach($match[1] as $var) {
$id = preUrl($var);
echo "<a href=userdetails.php?id='".$id."'><b>".$var."</b></a>";
}

chinakron

อ้างถึงจาก: EThaiZone ใน 07 มิถุนายน 2008, 19:44:46
function preUrl($output) {
if(php_version < 5) {   
$output = html_entity_decode($output,ENT_QUOTES);
} else {
$output = html_entity_decode($output,ENT_QUOTES,'UTF-8');
}
$output = str_replace("&", " and ", $output);
$out = array(";", ":", ".", "?", "=", "+", "$", "!", ">" , "<", "*", ")", "(", "[", "]", "^");
$output = str_replace($out, " ", $output);

return preg_replace("# +#", " ", trim($output));
}

$t = <<<EOT
<a href=userdetails.php?id=29209><b>(Ñ)it99bit(Ñ)</b></a>
<a href=userdetails.php?id=23719><b><->NGKOK<-></b></a>
<a href=userdetails.php?id=24222><b>bi@chy</b></a>

EOT;

preg_match_all('#<b>(.*)</b>#i', $t, $match);

foreach($match[1] as $var) {
$id = preUrl($var);
echo "<a href=userdetails.php?id='".$id."'><b>".$var."</b></a>";
}


-ขอบคุณมากครับ อุสาต์มาชี้แนะเองเลยหุๆๆ ขอไปลองเล่น ต่อละครับ กำลังเพลิน