PHP - Why Isn't This Str_replace Working?
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? Similar TutorialsI 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 all, I am at a loss to understand why this doesn't work. I have a simple function: function toHTML($inputText) { $inputText = str_replace('<','<',$inputText); $inputText = str_replace('>','>',$inputText); $inputText = str_replace('&','&',$inputText); $inputText = str_replace("'",''',$inputText); $inputText = str_replace('"','"',$inputText); return $inputText; } When I run code through it, specifically xml files from which I need to remove/replace the above charachters, it seems to refuse to replace all of the characters. For example, when I run the following text through this function, the '&' charachter I have highlighted is not replaced: <?xml version="1.0" ?> <!-- Purchase Order XML Copyright 2010 Version 1.0 --><PurchaseOrder> <Envelope> <SenderID>xxx</SenderID> <ReceiverID>xxx</ReceiverID> <DateStamp type="MessageDate">2011-02-09</DateStamp> <VersionID>1.4</VersionID> </Envelope> <Header> <Party type="SoldTo"> <IDNumber type="Supplier">xxx</IDNumber> <IDNumber type="Reseller">xxx</IDNumber> <Name>John Doe</Name> <AddressLine1>Temp</AddressLine1> <AddressLine2>Address 1</AddressLine2> <AddressLine3>Address 1</AddressLine3> <Street>Address 1</Street> <City>West Midlands</City> <PostCode>DY9 8QH</PostCode> <CountryCode>GB</CountryCode> <ContactPhone>01384444555</ContactPhone> </Party> <Party type="ShipTo"> <IDNumber type="Supplier">xxx</IDNumber> <IDNumber type="Reseller">DIRECTDROP</IDNumber> <Name> </Name><AddressLine1></AddressLine1> <Street></Street> <City></City><PostCode></PostCode> <CountryCode></CountryCode> <ContactPhone></ContactPhone> </Party> <!-- Kill/Fill 1-Back order allow. 2-Back order allow but ship complete. 3-Line item kill or fill. 4-whole order kill or fill. --> <KillOrFill>1</KillOrFill> <TotalValues> <Currency>GBP</Currency> <VATRate>20.0</VATRate> <MonetaryAmount type="FreightCharge">5.00</MonetaryAmount> <MonetaryAmount type="VATAmount">1</MonetaryAmount> <MonetaryAmount type="GoodsNetTotal">0</MonetaryAmount> <MonetaryAmount type="OrderNetTotal">5</MonetaryAmount> <MonetaryAmount type="GrossTotal">6</MonetaryAmount> </TotalValues> <OrderType>Direct Drop</OrderType> <DocumentReference> <DocumentNumber type="ResellerPO">J653</DocumentNumber> <DateStamp type="ResellerPO">2011-02-09</DateStamp> </DocumentReference> </Header> <Line> <Item> [b][color=red]<ProductName>Freecom Hard Drive Dock 2.5/3.5 USB&SATA</ProductName>[/color][/b] <ProductNumber type="Supplier">3294</ProductNumber> <Quantity type="Ordered">2</Quantity> <MonetaryAmount type="UnitPrice"></MonetaryAmount> <MonetaryAmount type="LineTotal">0</MonetaryAmount> </Item> </Line> </PurchaseOrder> The '&' between USB&SATA is not replaced with & on this line: <ProductName>Freecom Hard Drive Dock 2.5/3.5 USB&SATA</ProductName> Does anybody know why? Thanks in advance Hello,
I have multiple str_replace with file_put_contents in my script and it only seems to run the last str_replace.
If I remove the last one it runs the first one fine.
Code:
$file_name = "txt.txt"; the strange thing is...I have the exact same script on another part of my page and it works! the only thing that changed are the variable names and the path (which ive checked 27000 times) What this should do is check if you have a signature in the external file and if you do and you fill out the form correctly then it replaces your old signature with your new one. and if you dont have a signature already and filled out the form correctly then it should write your signature to the file...for some reason this isnt working after checking and rechecking...i need a second pair of eyes. Help please! if (isset($_POST['change_submit2'])) { if (($_SESSION['logged_in'] == '1') && ($signature_test != '1') && ($_POST['change_box2'] == 'CHANGE') && ($invalid_character_test != '1') && ($invalid_character_test1 != '1')) { $fh1 = fopen($users_signatures_path, 'r+'); fwrite($fh1, $sig_front); frwrite($fh1, $_POST['signature']); fwrite($fh1, $sig_back); fwrite($fh1, ' '); flose($fh1); echo 'You have succesfully created a signature!'; } elseif (($_SESSION['logged_in'] == '1') && ($signature_test == '1') && ($_POST['change_box2'] == 'CHANGE') && ($invalid_character_test != '1') && ($invalid_character_test1 != '1')) { $users_signatures_path1 = "../../users/users_signatures.txt"; $fh1 = fopen($users_signatures_path1, 'r+'); $read_fh1 = fread($_fh1, filesize($users_signatures_path1)); $the_new_sig = str_replace($mcurrent_sig, $mnew_sig, $read_fh1); fclose($fh1); $fh1 = fopen($users_signatures_path1, 'r+'); fwrite($fh1, $the_new_sig); fclose($fh1); echo $mnew_sig; echo 'Your signature has been succesfully changed!'; } elseif ($_SESSION['logged_in'] != '1') { echo 'You need to be logged in!'; } elseif ($_POST['change_box2'] != 'CHANGE') { echo 'You must fill in the "CHANGE" box!'; } elseif ($invalid_character_test == '1') { echo 'Contains illegal characters!'; } elseif ($invalid_character_test1 == '1') { echo 'Contains illegal characters!'; } } 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? I 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? 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... 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! 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 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 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 $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 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). 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\" /> 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 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! Hi all. I have a query that returns field names. I'm basically just trying to clean these field names up. Removing underscores, capitalizing letters, etc. The following is supposed to replace underscores with spaces, but it doesn't seem to work. Any help is appreciated. str_replace("_"," ",$fields->name); This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=331371.0 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. |