1,105,585 Community Members

str_replace and substr in one?

Member Avatar
mpc123
Junior Poster
160 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
GliderPilot
Posting Whiz in Training
296 posts since Sep 2006
Reputation Points: 31 [?]
Q&As Helped to Solve: 49 [?]
Skill Endorsements: 14 [?]
 
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>";
Member Avatar
mpc123
Junior Poster
160 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
urtrivedi
Posting Virtuoso
1,900 posts since Dec 2008
Reputation Points: 249 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 27 [?]
 
0
 
echo substr($txt7, 0, 200);
if(strlen($txt7)>200)
    echo "...";
Member Avatar
GliderPilot
Posting Whiz in Training
296 posts since Sep 2006
Reputation Points: 31 [?]
Q&As Helped to Solve: 49 [?]
Skill Endorsements: 14 [?]
 
0
 

code itself just echos out on to the page now though

Could you explain what you mean by this?

Member Avatar
mpc123
Junior Poster
160 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Currently nothing is coming out onto the page

Member Avatar
GliderPilot
Posting Whiz in Training
296 posts since Sep 2006
Reputation Points: 31 [?]
Q&As Helped to Solve: 49 [?]
Skill Endorsements: 14 [?]
 
0
 

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

Member Avatar
urtrivedi
Posting Virtuoso
1,900 posts since Dec 2008
Reputation Points: 249 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 27 [?]
 
0
 

write full code how / where u set $txt7

Member Avatar
mpc123
Junior Poster
160 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

they are in the head of the page

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

Member Avatar
GliderPilot
Posting Whiz in Training
296 posts since Sep 2006
Reputation Points: 31 [?]
Q&As Helped to Solve: 49 [?]
Skill Endorsements: 14 [?]
 
0
 

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

Member Avatar
urtrivedi
Posting Virtuoso
1,900 posts since Dec 2008
Reputation Points: 249 [?]
Q&As Helped to Solve: 409 [?]
Skill Endorsements: 27 [?]
 
0
 

if u echo $txt7 alone, do it printt anything

Member Avatar
diafol
Where are my eyes?
12,977 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,848 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
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.

Member Avatar
mpc123
Junior Poster
160 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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