PHP - How Do I Interrupt This Str_replace Routine?
Here is my form.It works fine.Because it is a radio button form, only one variable of the six available is chosen. This variable is echoed in the relevant hidden field. Then when the variable from the hidden field is posted, it is supposed to be converted to one of six specified strings and then posted to the database into column "egroup". The problem I have is that it always goes to the last variable and posts the string "egroup6". I want to break out of the str_replace routine and post the replacement name which matches the sequence of the hidden field selection.
How can I rewrite the str_replace routine so that it breaks when it finds the selected variable? The code for the form; Code: [Select] <table width="700" align="center" cellpadding="5"> <form action="adduser2.php" method="post" enctype="multipart/form-data"> <input name="egroup1" type="hidden" value="<?php echo "$group1"; ?>"> <input name="egroup2" type="hidden" value="<?php echo "$group2"; ?>"> <input name="egroup3" type="hidden" value="<?php echo "$group3"; ?>"> <input name="egroup4" type="hidden" value="<?php echo "$group4"; ?>"> <input name="egroup5" type="hidden" value="<?php echo "$group5"; ?>"> <input name="egroup6" type="hidden" value="<?php echo "$group6"; ?>"> <tr> <td colspan="3"><font color="#OO99CC"><?php echo "$errorMsg"; ?> </font></td> </tr> <tr> <td width="200"><div align="right"> <p><br /> Enter Student Name: </div></td> <td width="125"><p><br /> <input name="userId" type="text" value="<?php echo "$studName"; ?>" /> </td> <td width="350"> <input name="userPwd" type="hidden" value="<?php echo "$activatecode"; ?>" /> <font size="-1" color="#006600">(The password </font><font size="+2" color="#0000FF"><?php echo "$activatecode"; ?></font><font size="-1" color="#006600"> has been allocated to this next student. Print out your full list of student passwords <a href="../reports/reports_page.php">here</a>.)</font></td> </tr> <tr> <td></td> </tr> <tr> <td width="200"><div align="right">Allocate this student to <br /> one of these quiz groups:</div></td> <td width="125" colspan="1"><input type="radio" name="userGroup" value="'<?php echo "$group1"; ?>'" /> <?php echo "$group1"; ?><br /> <input type="radio" name="userGroup" value="'<?php echo "$group2"; ?>'" /> <?php echo "$group2"; ?><br /> <input type="radio" name="userGroup" value="'<?php echo "$group3"; ?>'" /> <?php echo "$group3"; ?><br /> <input type="radio" name="userGroup" value="'<?php echo "$group4"; ?>'" /> <?php echo "$group4"; ?><br /> <input type="radio" name="userGroup" value="'<?php echo "$group5"; ?>'" /> <?php echo "$group5"; ?><br /> <input type="radio" name="userGroup" value="'<?php echo "$group6"; ?>'" /> <?php echo "$group6"; ?><br /></td> <td width="350" align="right" ><p><?php echo $bottlinks; ?></td> </tr> <tr> <td><input name="userCollege" type="hidden" value="<?php echo "$school"; ?>" /> <input name="managerId" type="hidden" value="<?php echo "$userid"; ?>" /> <div align="right"></div></td> <td><input type="submit" name="Submit" value="Submit Form" /></td> </tr> </form> </table> When I post the data I use this code; Code: [Select] // Filter the posted variables $studName = preg_replace("/[^A-Z a-z0-9]/", "", $_POST['userId']); $activatecode = preg_replace("/^A-Z a-z0-9]/", "", $_POST['userPwd']); $school = preg_replace("/[^A-Z a-z0-9]/", "", $_POST['userCollege']); $userGroup = preg_replace("/[^A-Z a-z0-9]/", "", $_POST['userGroup']); // variables posted from hidden fields $egroup1 = $_POST['egroup1']; $egroup = str_replace($egroup1, "egroup1", $egroup1); $egroup2 = $_POST['egroup2']; $egroup = str_replace($egroup2, "egroup2", $egroup2); $egroup3 = $_POST['egroup3']; $egroup = str_replace($egroup3, "egroup3", $egroup3); $egroup4 = $_POST['egroup4']; $egroup = str_replace($egroup4, "egroup4", $egroup4); $egroup5 = $_POST['egroup5']; $egroup = str_replace($egroup5, "egroup5", $egroup5); $egroup6 = $_POST['egroup6']; $egroup = str_replace($egroup6, "egroup6", $egroup6); $managerid = preg_replace("/[^A-Z a-z0-9]/", "", $_POST['managerId']); // filter everything but spaces, numbers, and letters Here is the insert statement; Code: [Select] $sql = mysql_query("INSERT INTO users (userId, userPwd, userCollege, userGroup, egroup, managerId, doe) VALUES('$studName','$activatecode','$school', '$userGroup', '$egroup', '$managerid', now())") or die (mysql_error()); Similar TutorialsI'm allowing users to upload a file into a directory that is inside of my ftp's root directory(what is the proper term for this area anyway?) Anyway, I am uploading to this folder: Code: [Select] + www.mywebsite.com/ + files/ + images/ - welcome.jpg + system/ - text.txt - index.php + upload/ <-- here, this one right here - an_uploaded_file.zip If I have a php script that downloads from this folder would I need to worry about someone doing something that is not intended? I don't want someone overwriting my index.php with their own. I have a PHP application where I am trying to execute a PHP routine when the user clicks on a link to go to another page. The routine needs to execute before I use a header location command to send the user to the next location. I can't do this all with JavaScript, but it could be part of the solution if necessary. I'd prefer to just use PHP if I could, but I'm not sure that this is possible. Can anyone share with me a brief example of how I could do this? Thanks! 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? 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 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 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 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). 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! 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. 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? 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. 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. |