str_replace เปลี่ยนข้อความแค่ครั้งเดียวทำไงเหรอครับ

เริ่มโดย workshop, 03 มิถุนายน 2008, 01:04:15

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

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

workshop

ผมลองใช้ str_replace เปลี่ยนข้อความ แต่ว่ามันจะเปลี่ยนทั้งหมดเลย
ผมต้องการให้มันเปลี่ยนแค่ 1 หรือ 2 ครั้ง หรือตามตัวแปรที่กำหนดน่ะครับ ทำยังไงพอจะมีคำแนะนำไหมครับ

เช่น
aaaaaaaaa22ddddddddddddd22ffffffffffffffff22dfds
ต้องการเปลี่ยน 22 เป็น 5555555 ผลลัพธ์ที่อยากได้เช่น
เปลี่ยน 1 ครั้ง aaaaaaaaa5555555ddddddddddddd22ffffffffffffffff22dfds
เปลี่ยน 2 ครั้ง aaaaaaaaa5555555ddddddddddddd5555555ffffffffffffffff22dfds

ขอบคุณครับ

coconut

นานๆจะมีคนมาถามแบบนี้ครับ
ไม่รู้ว่าจะมีวิธีตรงๆมั้ยนะครับ ถ้าเกิดหาไม่เจอก้อเอานี่ไปก่อนแล้วกันครับ

<?
$time=1;
$a='aaaaaaaaa22ddddddddddddd22ffffffffffffffff22dfds';

function replace_time($input, $find, $replace, $time)
{
   preg_match_all("'(.*?)".$find."'si", $input, $items);
   $i=0;
   foreach($items[0] as $value)
   {
      if($i<$time)
      {
         $input=str_replace ( $value  , str_replace($find,$replace,$value)  , $input );
         $i++;
      }
   }

   return $input;
}

echo replace_time($a, '22','55555' ,$time);
?>

ball6847

ใช้ preg_replace ได้คับ มี พารามิเตอร์สำหรับกำหนด limit โดยตรง

อ้างถึงจาก: http://th.php.net/manual/en/function.preg-replace.php
mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit [, int &$count ]] )


<?php

$Txt 
"I like John Petrucci , John Petrucci is my favourite guitar hero.";
$Txt1 preg_replace("|John Petrucci|" ,"Paul Gilbert" $Txt);
$Txt2 preg_replace("|John Petrucci|" ,"Paul Gilbert" $Txt ,1);

echo 
$Txt "<br>";
echo 
$Txt1 "<br/>";
echo 
$Txt2 "<br/>";

?>



อ้างถึงจาก: output ของ โค้ดด้านบน
I like John Petrucci , John Petrucci is my favourite guitar hero.
I like Paul Gilbert , Paul Gilbert is my favourite guitar hero.
I like Paul Gilbert , John Petrucci is my favourite guitar hero.

ลองสังเกตุดูเอานะคับ
We use Ubuntu.

[direct=http://ng-seo.sourcelab.xyz/]AngularJS SEO Experimental[/direct]


workshop

ขอบคุณครับคุณบอล คุณมะพร้าว :)

ผมติดปัญหาว่า ต้นฉบับเป็น
New York cab driverKorben Dallas didn't mean to be a hero. But he just picked up the kind of fare that only comes along every five thousand years a perfect beauty, a perfect being, a perfect weapon. Now, together, theymust save the world. Bruce Willis, Milla Jovovich, and Gary Oldman star in acclaimed director Luc Besson's outrageous sci-fi adventure, an extravagantly styled tale of good against evil set in an unbelievable twenty-third century world. gps golf best seller book

มันจะกลายเป็น
The Fifth Element Sony Pictures The Fifth Element (Blu-ray) New York cab driverKorben Dallas didn\'t mean to be a hero. But he just picked up the kind of fare that only comes along every five thousand years a perfect beauty, a perfect being, a perfect weapon. Now, together, theymust save the world. Bruce Willis, Milla Jovovich, and Gary Oldman star in acclaimed director Luc Besson\'s outrageous sci-fi adventure, an extravagantly styled tale of good against evil set in an unbelievable twenty-third century world.

ball6847

เอิ้กๆ ใช้ str_replace ลบเอาอีกรอบละกันนะคับ  ;D

<?php

function replace_limit($search $replace $subject $limit=-1){
    return 
str_replace(array('\"',"\'") , array('"' "'") , preg_replace("|$search|"$replace $subject $limit));
}

?>
We use Ubuntu.

[direct=http://ng-seo.sourcelab.xyz/]AngularJS SEO Experimental[/direct]

workshop