We help IT Professionals succeed at work.

Function to Altering Text String

Medium Priority
258 Views
Last Modified: 2012-05-12
Hi experts !

I need a function to alter the content of an item description text. I want to insert a piece of text after the first "." found on the string. I want to do it 3 times, with the first 3 "." found.

Please see the code attached and see the image attached which is an example on how result should be.

Rewrite_Description($add1,$add2,$add3, $description)

Please help me with a good function to make possible this.

<?php 

$add1 = "The Author of this Book is Matt Green.";
$add2 = "This book was written on 1999.";
$add3 = "The Genero of this book is Fiction";

$description = "The drama was cultivated by the Incas, and dramatic performances were enacted before them. Garcilasso de la Vega, Molina, and Salcamayhua are the authorities who received and have recorded the information given by the Amautas respecting the Inca drama. Some of these dramas, and portions of others, were preserved in the memories of members of Inca and Amauta families. The Spanish priests, especially the Jesuits of Juli, soon discovered the dramatic aptitude of the people.";

?>

Open in new window

mod.jpg
Comment
Watch Question

Commented:
function Rewrite_Description($add1,$add2,$add3, $description)
{

$array = explode(".",$description);
$array[1] = $description.$array[2];
$array[2] = $description.$array[2];
$array[3] = $description.$array[2];

$newdescription = implode(".",$array);

return $newdescription;

}

Commented:
Sorry, I missed the part about different additions.  



function Rewrite_Description($add1,$add2,$add3, $description)
{
global $add1, $add2, $add3;

$array = explode(".",$description);
$array[1] = $add1.$array[1];
$array[2] = $add2.$array[2];
$array[3] = $add3.$array[3];

$newdescription = implode(".",$array);

return $newdescription;

}
Commented:
And you don't need the global.  

Note to self:  Read more carefully in the future. :)


function Rewrite_Description($add1,$add2,$add3, $description)
{

$array = explode(".",$description);
$array[1] = $add1.$array[1];
$array[2] = $add2.$array[2];
$array[3] = $add3.$array[3];

$newdescription = implode(".",$array);

return $newdescription;

}
Marco GasiFreelancer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Run script attached to see if it makes sense for you.

Cheers
<?php
$add1 = "The Author of this Book is Matt Green.";
$add2 = "This book was written on 1999.";
$add3 = "The Genero of this book is Fiction";

$adds = array($add1, $add2, $add3);

$description = "The drama was cultivated by the Incas, and dramatic performances were enacted before them. Garcilasso de la Vega, Molina, and Salcamayhua are the authorities who received and have recorded the information given by the Amautas respecting the Inca drama. Some of these dramas, and portions of others, were preserved in the memories of members of Inca and Amauta families. The Spanish priests, especially the Jesuits of Juli, soon discovered the dramatic aptitude of the people.";
echo $description ."<br />";

$array = explode('.', $description);

$line = 0;
for ($i= 0; $i < count($array); $i++){
    if ($i <=2){
        $array[$i] .= ". " . $adds[$i];
    }else{
        $array[$i] .= "."; 
    }
}

$newdescription = implode($array);
echo $newdescription;

?>

Open in new window

Top Expert 2013

Commented:
$inputSet=explode(".",$description);
$outputStr=$inputSet[0].$add1.$inputSet[1].$add2.$inputSet[2].$add3;
for ($i=3; $i< count($inputSet); $i++)
{
   $outputStr.=$inputSet[$i]. ' ';
}

Open in new window


You need to add a leading and trailing space to the three add strings, or the there will be incorrect spacing.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.