str-replace

PHP str_replace and preg_replace work on one server but not another

UPDATE: As it turns out, the below is caused by a caching issue on my production server. Thanks to everybody who contributed thoughtful answers. I have a simple function on a php page that takes a url such as: http://myurl.com/mypage.html?param1=value1 and converts it to: http://myurl.com/searchpage.html?param1=value1 All it does it...

PHP text formating: Detecting several symbols in a row

I have a kind of strange thing that I really nead for my text formating. Don't ask me please why I did this strange thing! ;-) So, my PHP script replaces all line foldings "\n" with one of the speacial symbol like "|". When I insert text data to database, the PHP script replaces all line foldings with the symbol "|" and when the script ...

I have string with "\u00a0" and I need to replace it with "" str_replace fails

I need to clean a string that comes (copy/pasted) from various office suite (excel, access, word) each with his own set of encoding. I'm using json_encode for debugging purposes in order to being able to see every single encoded character. I'm able to clean everything I found so far (\r \n) with str_replace, but with \u00a0 I have no l...

Replacing keywords in text with php & mysql

Hello, I have a news site containing an archive with more than 1 million news. I created a word definitions database with about 3000 entries, consisting of word-definition pairs. What I want to do is adding a definition next to every occurence of these words in the news. I cant make a static change as I can add a new keyword everyday, ...

str_replace between two numerical strings

Another problem with str_replace, I would like to change the following $title data into URL by taking the $string between number in the beginning and after dash (-) Chicago's Public Schools - $10.3M New Jersey - $3M Michigan: Public Health - $1M The desire output is: chicago-public-school new-jersey michigan-public-health PHP co...

php str_replace pattern

Hello , I have a php application that saves the pictures on the server and also stores the picture names in the database . The issue that I have is that the picture names include the path/folder where it was saved from (e.g 1220368812/chpk2198933_large-2.jpg) so I need a str_replace pattern to remove "1220368812/" and have the picture ...

str_replace() and strpos() for arrays?

I'm working with an array of data that I've changed the names of some array keys, but I want the data to stay the same basically... Basically I want to be able to keep the data that's in the array stored in the DB, but I want to update the array key names associated with it. Previously the array would have looked like this: $var_opts['s...

Skip first regex match

Hi, Is there anyway to skip the first match when using regex and php. Or is there some way of achieveing this using str_replace. Thanks UPDATE I am trying to remove all the instances of a string from another string but I want to retain the first occurance e.g $toRemove = 'test'; $string = 'This is a test string to test to removing t...

Escaping escape Characters

I'm trying to mimic the json_encode bitmask flags implemented in PHP 5.3.0, here is the string I have: $s = addslashes('O\'Rei"lly'); // O\'Rei\"lly Doing json_encode($s, JSON_HEX_APOS | JSON_HEX_QUOT) outputs the following: "O\\\u0027Rei\\\u0022lly" And I'm currently doing this in PHP versions older than 5.3.0: str_replace(array(...

LINQ and REGEX.REPLACE

I am trying to pull address records out of a database and group them together by address. Simple enough right? The problem I has is the LOCATION field is formatted as such BUILDING: some building description ADDRESS: 555 1st Street or BUILDING: some building description ADDRESS: 555 1st STREET There are multiple instances wher...

str_replace function wont replace string if i use single quotes

I using php's str_replace function to replace some text. Example below. str_replace("{x+{\$v}}", "{x-{\$v}}", $this->introtext); str_replace('{x+{\$v}}', '{x-{\$v}}', $this->introtext); In first case it replace text, but in second case it is not doing so. What is difference between two? ...

How to Ignore certain tags and replace texts in PHP

Hello, I have a variable like $content = "Lorem Ipsum is simply <b>dummy text of the printing</b> and typesetting industry. Lorem Ipsum has been the industry's <i>standard dummy text</i> ever since the 1500s <string>javascriptFunc();</script>" ; when i use str_replace('a', '', $content); all the 'a's get removed. But the 'a's with...

PHP, what is the better choice for removing a known string?

I am looking to search for and replace a known string from within another string. Should I use str_replace() or preg_replace()? The string to be replaced would be something similar to [+qStr+], [+bqID+], or [+aID+] and it would be being searched for in a code chunk similar to this: <li> [+qStr+] <ol class="mcAlpha"> <li><input...

Replace ' " in PHP

An interesting question about str_replace. Here's the example: $search = array('A', 'B', 'C', 'D', 'E'); $replace = array('B', 'C', 'D', 'E', 'F'); $subject = 'A'; echo str_replace($search, $replace, $subject); What if I want to replace '" into something else, say -. How do I do that? The problem is I can't write something like th...

str_replace match only first instance

A followup question to http://stackoverflow.com/questions/3063704/ Given the following POST data: 2010-June-3 <remove>2010-June-3</remove> 2010-June-15 2010-June-16 2010-June-17 2010-June-3 2010-June-1 I'm wanting to remove ONLY the first instance of 2010-June-3, but the following code removes all the data. $i = 1; $pattern = "/<rem...

PHP str_replace/preg_replace problem with php open tags

I'm trying to replace something like: $text = "Hello <--name--> !!"; echo str_replace("--","?",$text); Expected: Hello <?name?> !! Result: Hello !! (I'm checking the source code, and I have short open tags enabled) I have tried so many ways but it seems that I can't never have as result any <? (or <?php) string. I think it may ...

Replace a char into NSString

Hi all! I want simply replace all occourrencies of "+" with a blank " " char... I tried some sample listed here, also used NSSMutableString, but the program crash... what's the best way to replace a char from another?? thanks ...

Simple template var replacement, but with a twist

So I'm setting up a system that has a lot of emails, and variable replacement within it, so I'm writing a class to manage some variable replacement for templates stored in the database. Here's a brief example: // template is stored in db, so that's how this would get loaded in $template = "Hello, %customer_name%, thank you for contact...

[PHP] Can't remove special characters with str_replace

Hi, I have a very trivial problem with str_replace. I have a string with the En Dash character ( - ) like this: I want to remove - the dash The html output is I want to remove the &ndash; the dash I want to do this: $new_string = str_replace ('-','',$string); I've tried to parse the string with html_entity_decode, to parse the ...

str_replace within certain html tags only

Hello all, first post woo! I have an html page loaded into a PHP variable and am using str_replace to change certain words with other words. The only problem is that if one of these words appears in an important peice of code then the whole thing falls to bits. Is there any way to only apply the str_replace function to certain html tag...