We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,386 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

str_replace and substr in one?

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

4
Contributors
12
Replies
22 Hours
Discussion Span
5 Months Ago
Last Updated
13
Views
Question
Answered
mpc123
Junior Poster
114 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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>";
GliderPilot
Posting Whiz in Training
239 posts since Sep 2006
Reputation Points: 34
Solved Threads: 42
Skill Endorsements: 12

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

mpc123
Junior Poster
114 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
echo substr($txt7, 0, 200);
if(strlen($txt7)>200)
    echo "...";
urtrivedi
Posting Virtuoso
1,715 posts since Dec 2008
Reputation Points: 299
Solved Threads: 362
Skill Endorsements: 24

code itself just echos out on to the page now though

Could you explain what you mean by this?

GliderPilot
Posting Whiz in Training
239 posts since Sep 2006
Reputation Points: 34
Solved Threads: 42
Skill Endorsements: 12

Currently nothing is coming out onto the page

mpc123
Junior Poster
114 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

GliderPilot
Posting Whiz in Training
239 posts since Sep 2006
Reputation Points: 34
Solved Threads: 42
Skill Endorsements: 12

write full code how / where u set $txt7

urtrivedi
Posting Virtuoso
1,715 posts since Dec 2008
Reputation Points: 299
Solved Threads: 362
Skill Endorsements: 24

they are in the head of the page

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

mpc123
Junior Poster
114 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

GliderPilot
Posting Whiz in Training
239 posts since Sep 2006
Reputation Points: 34
Solved Threads: 42
Skill Endorsements: 12

if u echo $txt7 alone, do it printt anything

urtrivedi
Posting Virtuoso
1,715 posts since Dec 2008
Reputation Points: 299
Solved Threads: 362
Skill Endorsements: 24

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.

diafol
Keep Smiling
Moderator
10,672 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57

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

mpc123
Junior Poster
114 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 5 Months Ago by GliderPilot, urtrivedi and diafol

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0966 seconds using 2.73MB