Welcome to Dream.In.Code
Become a PHP Expert!

Join 136,926 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,816 people online right now. Registration is fast and FREE... Join Now!




Replacing part of string

 
Reply to this topicStart new topic

Replacing part of string, (or editing maybe)

mudisoft
14 Mar, 2008 - 01:11 PM
Post #1

New D.I.C Head
*

Joined: 9 Jul, 2007
Posts: 11


My Contributions
Hello, guys!

I'm wondering if there's any way to replace a part of a string or to change it a little in PHP.

Let's assume that I have the following:
First, the string
$string = "smth;smthelse;thirdpart;blahblah;andsoon";

Now I want to bold the fourth part of my string and make it <b>blahblah</b> so the end result would be as follows
$string = "smth;smthelse;thirdpart;<b>blahblah</b>;andsoon";

I thought of exploding my delimited string to an array, but the number of parts (5 in the example) varies and I don't know what to do...
I would be most grateful if you share some ideas smile.gif
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Replacing Part Of String
14 Mar, 2008 - 01:47 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
You have str_replace that will replace it for you.

str_replace() at php.net

Enjoy! smile.gif
User is online!Profile CardPM
+Quote Post

mudisoft
RE: Replacing Part Of String
15 Mar, 2008 - 11:13 AM
Post #3

New D.I.C Head
*

Joined: 9 Jul, 2007
Posts: 11


My Contributions
Thank you!
I wonder how I missed it on PHP.net confused.gif

Works great that way if somebody is wondering:
CODE
// Provides: You should eat pizza, beer, and ice cream every day
$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");

$newphrase = str_replace($healthy, $yummy, $phrase);

User is offlineProfile CardPM
+Quote Post

spearfish
RE: Replacing Part Of String
15 Mar, 2008 - 02:32 PM
Post #4

Monkey in Training
Group Icon

Joined: 10 Mar, 2008
Posts: 746



Thanked: 2 times
Dream Kudos: 225
My Contributions
Might I ask what this is for?
User is offlineProfile CardPM
+Quote Post

mudisoft
RE: Replacing Part Of String
16 Mar, 2008 - 10:52 AM
Post #5

New D.I.C Head
*

Joined: 9 Jul, 2007
Posts: 11


My Contributions
Well, if you try running the example you'll get a better idea...
str_replace() finds a string in a string and changes it with another one. I needed to dynamically bold a title in a string of titles and that did the job perfectly wink2.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 10:24PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month