PHP - Str_replace Help
I am trying to learn PHP and am a novice so please excuse the stupid question but I have researched and attempted to find a solution for this and cant. Hoping someone can help me.
I have the following code I am using within a Wordpress template... Code: [Select] <?php if (function_exists('simple_feed_list')) : ?> <?php $title = get_the_title(); str_replace("+", " ", $title); simple_feed_list('http://www.amazon.com/rss/tag/'. str_replace(" ", "+", $title) .'/new/ref=tag_tdp_rss_new_man?tag=search-20','limit=5&desc=yes&nofollow=yes&more=no'); ?> <?php endif; ?> I am trying to make it produce a RSS feed url from amazon that looks like this: http://www.amazon.com/rss/tag/ipod ibuds/new/ref=tag_tdp_rss_new_man?tag=search-20 note the space between ipod and buds, that is what I am trying to accomplish. Right now the space is being changed I think to a + sign which breaks the feed if the search term is more than one word. Can anyone help me fix this? I have tried removing the string replace part, etc with no luck Similar TutorialsI want to basically delete any word that ends with ... So.. If I have the following phrase: Elvis Pres... I want to remove the word Pres... entirely. Basically, something along the lines of... $myphrase = str_replace("Pres...",'',$myphrase); except that I never know what $myphrase will be, so the above won't work. Is it str_replace I need or something else? Hello friends, If we have link $x = "search engin"; <a href="google.com"><?=$x?></a> and we want to remove space btween (search engine) to be (search-engine) ineed we should use str_replace and we change the link to $manal = str_replace(" ", "-", trim($x)); <a href="google.com"><?=$manal?></a> It not working ? and only working if $manal = str_replace(" ", "+", trim($x)); why + not - ? and is there anyway else to replce space to - ? thanks Hello All, I'm using php to auto-insert variables into a canonical url within the <head>. Here is my code: <link rel="canonical" href="http://www.mysite.com/page.php?IT=<?php print $item; ?>&IT2=<?php print $desc; ?>" /> I used this to replace "+" with " ". <?php $desc = str_replace ("+"," ",$desc); ?> The problem is i use the variables in different sections of the page were I don't want the "+" to appear. I only want the "+" to appear in the canonical url space. I'm sure there is an easy way, but not for me So to sum up: I want srt_replace code to be incorporated into the <?php print $desc; ?> code ONLY. Thanks in advance for any help! Hi... I read on php.net manual that its better to use str_replace than preg_replace. I used Code: [Select] $summary = clean($_POST['summary']); $summary = nl2br($summary); to convert the carriage return(is that the correct term?) to insert them into mysql. So naturally, i want them converted into <br> when i pull them from mysql. I used this: Code: [Select] $num_rows=mysql_num_rows($result); if($num_rows == 0){ } else{ for($x = 0; $x < $num_rows; $x++){ $row = mysql_fetch_assoc($result); $id = $row['id']; $position = $row['position']; $summary = $row['summary']; $order = array("\r\n", "\n", "\r"); $replace = '<br />'; $newstr = str_replace($order, $replace, $summary); But i got this error: Code: [Select] Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/...php on line 310 Is it a syntax error? Thanx in advance... $to_censor = array("f", "a", "e", "r", "c"); $new_censor = array("****", "****", "****", "****", "****"); $new = str_replace($to_censor, $new_censor, $description); Is it possible to do this without having to add a new **** in $new_censor when I add another censored word in $to_censor more test -- ================================================================== this mobile text message is brought to you by at&t How can I get the red part removed? I tried: $endings = array('\n--\n==================================================================\nthis mobile text message is brought to you by at&t'); $CONTENT = str_replace($endings,"",$CONTENT); i put it in an array because i'll have to add more things later I sometimes have words thrown into my database with the following symbol: Code: [Select] … What I'm trying to achieve is this.. Anytime that symbol is found, replace it and all previous letters.. back to the first space encountered... with nothing. Make sense? If I had the sentence Code: [Select] "the lazy brown d..…, it should return for me "the lazy brown" If I was just replacing the weird code, it would be a simple str_replace, but not sure how to make the letters before the symbol (up to the first space) go away. Ideas? How can I add str_replace(" ", "-") to the following line: Code: [Select] echo "<p>" . $_SERVER['SERVER_NAME'] . "/" . strtolower(stripslashes($row['name'])) .".php</p>"; I have a bunch of provinces being displayed and want to replace spaces with - (for example British Columbia turns into www.domain.com/british-columbia.php so I can link to this file). Here's my code, a simple replace $newlink = str_replace("/http:\/\/images\.4chan\.org\/(.+)\/src\//", "thread/", "http://images.4chan.org/adv/src/1288558759794.png"); Print $newlink; I want it to output "thread/1288558759794.png", but its just giving me the link I started with. Any ideas whats wrong? I have a row in my sql I had to remove quotes from a href links to import it into my database because it was a csv file and delimiters were quote so I made a script to remove them. But I made a script to remove them: $file = file_get_contents('ibf_forums.csv'); $file = str_replace('="','=^69^',$file); $file = str_replace('\"','\^69^',$file); $file = str_replace('"\ ','\^69^',$file); Now I need to put them back in. So I need to replace ^69^ in a database. If I use UPDATE it will over write it all without replace just a part of it. An example if I don't make sense: Code: [Select] Thanks! <img src=\^69^http://domain.com/style_emoticons/<#EMO_DIR#>/tongue.gif\^69^ style=\^69^vertical-align:middle\^69^ emoid=\^69^:P\^69^ border=\^69^0\^69^ alt=\^69^tongue.gif\^69^ />Into this: Code: [Select] Thanks! <img src=\"http://domain.com/style_emoticons/<#EMO_DIR#>/tongue.gif\" style=\"vertical-align:middle\" emoid=\":P\" border=\"0\" alt=\"tongue.gif\" /> I am trying to make a presentation generator, using information from my database; my str_replace is not working please help.... here is my code Code: [Select] <?php $Sql = "SELECT * FROM csm_players WHERE uid = '{$this->Uid}'"; $Query = mysql_query($Sql); $Style = $this->UserInfo['playerstyle']; while ($p = mysql_fetch_assoc($Query)) { $name = explode(" ",$p['name']); $PlayerName = (strpbrk($Style, '{FIRSTNAME}') ? str_replace("{FIRSTNAME}",$name[0],$Style) : ""); $PlayerName1 = (strpbrk($Style, '{LASTTNAME}') ? str_replace("{LASTTNAME}",$name[1],$PlayerName) : ""); $PlayerName2 = (strpbrk($Style, '{NICKNAME}') ? str_replace("{NICKNAME}",$p['nickname'],$PlayerName1) : ""); if ($p['cl'] == 1) { if ($this->UserInfo['clba'] == 1) { $Show .= "<div>{$this->UserInfo['clstyle']} {$PlayerName}</div>"; } else { $Show .= "<div>{$PlayerName} {$this->UserInfo['clstyle']}</div>"; } } else { $Show .= "<div>{$PlayerName}</div>"; } } //$Show .= "Error"; return $Show; this is what its returning Shelton "{NICKNAME}" {LASTNAME} [CL] Lindsey "{NICKNAME}" {LASTNAME} Eitan "{NICKNAME}" {LASTNAME} Aake "{NICKNAME}" {LASTNAME} Hunter "{NICKNAME}" {LASTNAME} if i take out the firstname search then it find the nickname and if i take out the nickname and the first it shows the last one.... what do i do? i have tried to put them in an array and do a foreach with keys and val but that doesnt work either im at a standstill. it works for my other script but not for this... Hi, I have the following text: Quote London, May 1: Hi there I am trying to remove everything till ':' The desired output is Quote %20Hi%20there I did this - Quote $np2 = "London, May 1: Hi there"; $i = array('-',' ','(.*):'); $j = array('','%20',''); $np = str_replace($i, $j, $np2); echo $np; I am unable to remove Quote London, May 1: Note: The word "London" could be anything, so I am trying to get a wildcard. I am trying to change the data in a .xml document. I am pulling contact infomation from a database and adding it to my phone system's speed dial list. I got everything else to work except this... My code looks like: $fileurl = 'data2.xml'; $rc = (starts with 300 and adds 1 with every loop) $misc = (11 digit phone number with no spaces (ex 14025551212)) $fullname = (first and last name of customer) $search = $rc."</c1> \n <c2 /> \n <c3 />"; $data = $rc."</c1> \n <c2>9".$misc."</c2> \n <c3>".$fullname."</c3> "; $file_contents = file_get_contents($fileurl); $fh = fopen($fileurl, "w"); $file_contents = str_replace($search,$data,$file_contents); fwrite($fh, $file_contents); fclose($fh); The Data in the data2.xml file looks like this: - <item row="100"> <c0>100</c0> <c1>*300</c1> <c2 /> <c3 /> </item> - <item row="101"> <c0>101</c0> <c1>*301</c1> <c2 /> <c3 /> </item> ...ect with rows going up to 999 And I need it changed to look like this: - <item row="100"> <c0>100</c0> <c1>*300</c1> <c2>14025551212</c2> <c3>John Smith</c3> </item> - <item row="101"> <c0>101</c0> <c1>*301</c1> <c2>14025552121</c2> <c3>Jane Smith</c3> </item> ...ect with rows going up to 999 I am looking at replacing what I searched for ($search) and change it to the data I get from my database ($data). It seems like when I search for <c2 /> it errors like there is something stopping it. The problem is the format of the data.xml file, but I can not change it. If I leave off the <c2 /> and <c3 /> the code works fine but looks like this: - <item row="100"> <c0>100</c0> <c1>*300</c1> <c2>14025551212</c2> <c3>John Smith</c3> <c2 /> <c3 /> </item> I got to get rid of the <c2 /> and <c3 /> and replace it with my data. Also eventually I will have less contacts and need to revert the data back to the original format. I have tried everything I know and no success. Anyone got any ideas that would work? I am thinking its something simple that I am overlooking. Thanks! Hello. I am pulling data from a mysql table and want to replace some strings that may appear in the data. Is it possible to use str_replace to replace more than one value at a time? For instance, replace '+' with 'plus' AND '-' with 'minus'. This doesn't work but something like this -- str_replace($search1, $replace1, $subject1, $search2, $replace2, $subject2); Thanks for all help! What I want to do should be very simple, but I've stared at code all day and can't grasp it. I want to replace the "&" character, if it exists, with the "&" string. What I have is: Code: [Select] $name = $RESULT['ACCOUNT_NAME']; $cleaned = str_replace('&','& ', $name); echo $cleaned; It doesn't crash or anything, but it doesn't work. I have several ampersand (&) characters in my output that aren't getting replaced. Any help is appreciated. This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=331371.0 I am making script that will include 2 files , 1. with the cracked hashes and 2. with the usernames and hashes 1. file is like : hash:salt:password , and 2. file is like is username:hash:salt I want that the script loop through the 1.. file with hash:salt:password and replace every hash:salt with the password in 2. file I have written something but it isnt working for me Code: [Select] <?php $usernames=file_get_contents('users.txt'); $cracked=file('cracked.txt'); foreach($cracked as $cracked_line){ $hashes_explode=explode(":",$cracked_line); $hash_salt="$hashes_explode[0]:$hashes_explode[1]"; $plain_password=str_replace($hash_salt,$hashes_explode[2],$usernames); print $plain_password; } print $plain_password; ?> Hello guys this is my first post. Just a quick problem I've encountered, I'm trying to str_replace an array and string together and wondering how it can be done? Code: [Select] $text = "this is @value1 and this is @value2"; $array1 = array ( 0 => 'value1', 1 => 'value2'); // just an example $at_replace = str_replace (" . @ " $array1, " . # " $array1, $text); So im basically trying to replace all instances in $text where each word in $array1 starting with '@' will be replaced with a '#'. Also will this be better done with a loop? Any help or direction is greatly appreciated. I am trying to replace some fields in a file using the following code Code: [Select] $test = array( 'NAME' => 'A N Other', //$mailName, 'MAIL' => 'someone@internet.com', //$mailEmail, 'TELE' => '01111111111', //$mailTel, 'BODY' => 'This is a test message', //$mailBody, 'TIME' => date('g:ia'), 'DATE' => date('l jS F Y'), ); $file = file_get_contents("./languages/en/messeges/new_message.txt"); $value = array(); foreach($test as $key => $val) { $value[] = str_replace('{'.$key.'}', $val, $file); } $file ="<pre>$file</pre>"; echo implode($file, $value); here is the contents of "./languages/en/messeges/new_message.txt" Quote New message received on {DATE} at {TIME} From: {NAME} Contact Number: {TELE} Email: {MAIL} ---------------------------------------------------------------------------- {BODY} ---------------------------------------------------------------------------- Can someone please help as its echoing the complete file for every instance, 1 at a time |