?
Solved

Having problem with php str_replace function

Posted on 2013-05-14
2
Medium Priority
?
362 Views
Last Modified: 2013-05-14
Hi
I'm have a issue with the php function str_replace.

I have the following block of text from with I want the function to find
s:26 "Simple Design with 1 Image" and replace it with s:19 "Design with 1 Image"

a:1:{s:1:"a";a:1:{i:1;a:3:{s:2:"id";s:1:"1";s:4:"name";s:8:"Artwork?";s:7:"options";a:4:{i:0;a:2:{s:2:"id";s:1:"1";s:4:"name";s:22:"Supplying Own [¿0]ash";}i:1;a:2:{s:2:"id";s:1:"2";s:4:"name";s:34:"Text Only with Simple Fonts a[¿1]";}i:2;a:2:{s:2:"id";s:1:"3";s:4:"name";s:26:"Simple Design with 1 Image";}i:3;a:2:{s:2:"id";s:1:"4";s:4:"name";s:35:"Complex Design with Multiple Images";}}}}}

the functions work if I just replace Simple Design with 1 Image but when I have the whole string with double quotes it doesn't work.

This is the code i'm using and $row[0] is the above block of text that is pulled from the database
$strfind1 = "s:26 \"Simple Design with 1 Image\"";
$strrep = "s:19 \"Design with 1 Image\"";

$newdata = str_replace($strfind1, $strrep, $row[0]);

Open in new window


Thank you in advance for any help
0
Comment
Question by:ACEAFTY
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 2000 total points
ID: 39164047
looking at the text being searched ($row[0]) i can see that there's : between s:26 and the text Simpla Desing, while you are looking for a space between them.
so basically this is what you should replace:
s:26:"Simple Design with 1 Image"
and not
s:26 "Simple Design with 1 Image"

so try this instead:
$strfind1 = "s:26:\"Simple Design with 1 Image\"";
$strrep = "s:19:\"Design with 1 Image\"";

$newdata = str_replace($strfind1, $strrep, $row[0]);

Open in new window

0
 
LVL 1

Author Closing Comment

by:ACEAFTY
ID: 39164066
Thank you.
Such an obvious mistake, can't believe I missed it.
0

Featured Post

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question