1.11M Members

str_replace and substr in one?

 
0
 

Hi I have an issue whereby I need to replace characters in a field and also narrow the amount of characters down, but sturggling with putting them both together

I have 2 seperate lines of code to do them both seperately that work, but struggling to get them together btw $txt7 is also the field description, its just the way it has ended up trying different ways to get them together :)

'. str_replace('*','' . $file['field2'] . '',trim($file['description'])) . '<p>

<?php

if(strlen($txt7)>200){
echo substr($txt7, 0, 200) . '…';
}
else {
echo $txt7;
}

?>

Hope you can help

Thanks

 
0
 

I think I understand what you're trying to do.

$raw_text = trim($file['description']);

if(strlen($raw_text)>200) { $raw_text = substr($raw_text, 0, 200) . "..."; }

$final_text = str_replace('*', $file['field2'], $raw_text);

echo "<p>$final_text</p>";
 
0
 

Hi thanks for your help

the code itself just echos out on to the page now though?

This is the bit I need to edit really if you can help with that

<?php
if(strlen($txt7)>200){
echo substr($txt7, 0, 200) . '…';
}
else {
echo $txt7;
}
?>

Thanks

 
0
 
echo substr($txt7, 0, 200);
if(strlen($txt7)>200)
    echo "...";
 
0
 

code itself just echos out on to the page now though

Could you explain what you mean by this?

 
0
 

Currently nothing is coming out onto the page

 
0
 

How are file['description'] and file['field2'] being set? I would need to see the rest of the code

 
0
 

write full code how / where u set $txt7

 
0
 

they are in the head of the page

<?php$txt2 = "' . $file['field2']. '"; $txt7 = "' . $file['description']. '"?>

 
0
 

That still doesn't explain where your $file['field2'] and $file['description'] array is being set?

 
0
 

if u echo $txt7 alone, do it printt anything

 
2
 

How about the mb_* functions:

echo mb_strimwidth($txt7, 0, 203, "...");

You need to add the number of characters in the append parameter (#4) to the end trim parameter (#3). Start trimming on parameter #2, using the string #1.

 
0
 

I couldnt get it to work so just set up another field and updated with same info and used 2 fields.

Thanks for help anyway

Question Answered as of 1 Year Ago by GliderPilot, urtrivedi and diafol
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article