PHP - Remove Characters From A String Starting At A Specific Character
I have a form with some dropdownlist that get populated dynamically(depends on what the user chooses) with jquery and an xml file. In the form I have a button that takes the info and populates another form with the values of the ddl. I have coded script which populates the second form with jquery and the name of each input get dynamically added like so:
Code: [Select] <input type="text" value="'+model+'" name="Model_'+model+'" id="'+model+'" readonly size="'+model.length+'" /> <input type="text" value="'+model+'" name="Model'+model+'" id="'+model+'" readonly size="'+model.length+'" />> <input type="text" value="'+color+'" name="Color'+color+'" id="'+color+'" readonly size="'+color.length+'" /> <input type="text" value="'+part+'" name="Part'+part+'" id="'+part+'" readonly size="'+part.length+'" /> <input type="text" value="$'+price+'" name="Price'+price+'" id="'+price+'" readonly size="'+price.length+'" /> <input type="text" value="'+qty+'" name="Quantity'+qty+'" id="'+qty+'" readonly size="'+qty.length+'" /> <input type="text" value="'+total+'" name="Total'+total+'" id="'+total+'" readonly size="'+total.length+'" /> What I want to do is grab the name of the element and remove the underscore and everything else form rileft to right so I am left with just the word before the underscore. I tried using substr($key,0,-28) but that wont work since everything after the underscore will have a different length. I was wondering if there is a way i can keep everything before the underscore and remove the underscore and everything after that. Thanks in advance for any input I get. Similar TutorialsAm looking forward to removing all the numeric characters upto and including the leading "/" in 90/_featuredarticles/2011/12 Regards Hi, Im looking at using a statement like this to remove all text characters from a string. $outgoing = substr_replace($incoming,"",-1); The string is a currency that has a currency symbol in the beginning of the string, like "R500.00" I would like to remove the R, so it is just a numeric value... Please can you help me with any ideas on how to get this going? Been reading so much and its all confused me too badly. Regards, Chris I have sen this function before but can not remember what it is or how to code it. It allows you remove only defined characters from the end of the string. I am not looking to replace any occurrence anywhere in the phrase, just the end. For example lets say I want to remove"ld!" from phrase "Hello World!" That function would return "Hello Wor" Thanks This is probably a simple one. I just couldn't find a tutorial to the specific thing I'm trying. I have a string that looks like: "(abbreviated name with.) second abbrev. name." I'm trying to find a way to break this apart so I can have: $var1 = "abbreviated name with." $var2 = "second abbrev. name." I've looked at substr and a couple others and I'm not sure quite how to do this, because the ')' could be in a differing location. I can't for the life of me understand how to perform this so I need some professional help here. I have a large array that contains some garbage every Nth and Nth + 1 element that needs to go away. array_filter doesn't work with keys but anyhow, i wouldn't know how to design the function. My keys are numeric, starting from 0. The keys I want to remove are part of this sequence: 8, 9, 18, 19, 28, 29 ... Can someone help me design a generic function to remove these keys? function filter_keys($array, $n, $offset) {} $new_array = filter_keys($array, 10, 8 ); I would maybe have to run this function twice, once for 8 offset and once for 9 offset. Why do I get the following results (specifically the second example)? I thought it would be documented under https://www.php.net/manual/en/language.types.string.php, however, the word "ampersand" doesn't appear. Also, looked at logical operators, but found nothing. Thanks function test(string $string){ printf('%s %s %s 0'.PHP_EOL, gettype($string), $string, $string==0?'==':'!='); } test('123'); test('@123'); test('1@23'); test('123@');Quote
string 123 != 0
Hi, I believe this is my first post on here. First off let me say thank you for silently helping out in the past while I keep on learning PHP. I have decided to build an image gallery, and I am having problems with the page numbering links and the breadcrumbs. Reson I decided to build my own is that I wanted a challange to learn more. I could not find anything that would intregrate with the site as I wanted it to (pulled as a function so offers some customising with options). The problem that I am having is that the <a href= tag is starting to give multipul "/"s when I only need one, so that the url at the top is coming up as http://localhost/mmv4/gallery/pics///Ax/Ax_Jo/ when I am wanting http://localhost/mmv4/gallery/pics/Ax/Ax_Jo/IMG_4244.JPG. I have used str_replace to catch //s and more, but this then adds odd slashes on the end so the page numbeing is getting odd results. some dirs have the / on the end and some do not, so the links are sometimes "visited" and others not. I beleve that the problem is generated in Code: [Select] <?php if(isset($_GET['album'])) { //load the available dirs into an array so that the sneeky users cannot get further back. $files = scandir($gal); $dirs = array(); foreach($files as $file){ if($file != '.' && $file != '..' && $file != 'thumbs') { if(is_dir($gal."/".$file)) { $dirs[] = $file; //echo $file ."<br />\n"; } } } //if(in_array($_GET['album'],$dirs)) { $album = "/".$_GET['album']."/"; $album = str_replace("..", "", $album); //} //else { // $output .= "<p>There is no album called \"$_GET[album]\", please select an album from below.</p>"; //} } or the breadcrumbs bit Code: [Select] <?php if($crumbs == 1) { //bread crums $path = $gal.$album; $path = explode("/", $path); $crumbs = array(); foreach($path as $key => $value) { if($value != ""){ $crumbs[] = $value; } } // create Breadcrumbs div $output .= "<div id=\"breadcrumbs\">"; $number = count($crumbs); $real = ""; foreach($crumbs as $num => $link) { if ($link == $gal) { $link = "Gallery Home"; } if ($num == $number -1) { $output .= $link; } else{ if ($num >= 1){ $real =""; for($x=1; $x<=$num; $x++) { $real .= "/".$crumbs[$x]; } $output .= "<a href=\"?album=".str_replace("/", "",$real)."\">".$link."</a>"." <- "; } else{ $output .= "<a href=\"?album=".str_replace("/", "",$real)."\">".$link."</a>"." <- "; } } $num +1; } //close breadcrumbs div $output .= "</div>"; } Can any one please help me shed some light on this one, I am confused. I can send / attach the full source if needed. MOD EDIT: tags fixed for syntax highlighting . . . I submit text to my MySQL database using a form on my site. When I look at the text in my database, sometimes, there are strange characters. The strange characters are caused by quotation marks, em dashes, apostrophes and foreign letters of the alphabet. I think that this only happens when the source of the text is a Windows program. I understand that this is a character encoding issue, but I don't fully understand the subject. I've spent the last few hours researching it, but it's only confused me.
My site uses UTF-8 encoding:
<meta http-equiv="content-type" content="text/xml; charset=utf-8" />The collation of my database is utf8_general_ci. My form looks like this: <form action="" method="post"> </form>As you can see, an accept-charset="utf-8" attribute has not been specified. Questions 1) I am guessing that my problem is that the Windows characters are being misinterpreted by my UTF-8 setup. Is that correct? 2) If so, is there a way that I can safely convert the Windows characters to UTF-8 during the submission process? 3) Should I also specify an accept-charset="utf-8" attribute on the form? 4) When I paste the Windows text directly into my database without using the form, the characters save without turning into the strange characters. But they don't render properly on my site. Can't browsers identify Windows characters? Hiya peeps, I am using preg_match to validate URLS; preg_match("/(((https?|ftp|gopher):\/\/|(mailto|file|news):)[^' <>\"]+|(www|web|w3).[-a-z0-9.]+)[^' .,;<>\":]/i", $this->_searchString) I works great, the only issue I have is I am using it in an if statment, and what I need to do is something like this. if(preg_match("/(((https?|ftp|gopher):\/\/|(mailto|file|news):)[^' <>\"]+|(www|web|w3).[-a-z0-9.]+)[^' .,;<>\":]/i", $this->_searchString)) { CHECK IF $this->_searchString has http:// at the start of it or if it has a / at the end and remove them if it does. I need $this->_searchString to end up with only www.SITE.co.uk before I can input it into the relevant function. } else { } Many thanks, James. Hey... I'm trying to echo the title of the news in the right column but I just want to display maximum of 25 characters how can I do it? thanks Hi all, I have an array of data. I am looping through this data to generate a CSV file. However, if there are commas in any of the elements then this will throw out the structure of the CSV file. My question is, how do I loop through the array before I use it to strip out any commas and replace with a space? Thanks for your help. This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=356719.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=322184.0 What am I doing wrong? <?PHP echo preg_replace('#(<td class="results" style="border-width: 0px 0px 1px;">).*?(</td>)#', 'test', $raw_data); ?> If the contents of $raw_data has: Code: [Select] <td class="results" style="border-width: 0px 0px 1px;">THERE IS SOME CONTENT HERE</td> should it not be replaced as so? : Code: [Select] <td class="results" style="border-width: 0px 0px 1px;">test</td> Hi all! I am building a website, and the users can create a pet name. I only want them to be able to use letters, numbers and underscores in the names. How can I do this? Thanks! I would also be using this for the usernames as well. Here is my pet creation code: adopt.php: Code: [Select] <?php session_start(); include("config536.php"); ?> <html> <head> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <?php if(!isset($_SESSION['username'])) { echo "<banner></banner><ubar><a href=login.php>Login</a> or <a href=register.php>Register</a></ubar><content><center><font size=6>Error!</font><br><br>You are not Logged In! Please <a href=login.php>Login</a> or <a href=register.php>Register</a> to Continue!</center></content><content><center><font size=6>Messages</font><br><br></center></content>"; } if(isset($_SESSION['username'])) { echo "<banner></banner><nav>$shownavbar</nav><ubar>$ubear</ubar><content><center><font size=6>Adopt a Pet</font><br><br>"; $thedateis = date('F jS, Y'); $getpetid = $_GET['petid']; $adopt = $_POST['submit']; $petname = $_POST['petname']; $petgen = $_POST['gender']; $thebq = "SELECT * FROM pets WHERE petid='$getpetid'"; $theb = mysql_query($thebq); while($prr = mysql_fetch_array($theb)) { $pid = $prr['petid']; $species = $prr['pname']; $number = $prr['number']; } if(!isset($getpetid)) { $pq = "SELECT * FROM pets WHERE petid != '0'"; $p = mysql_query($pq); while($pr = mysql_fetch_array($p)) { $pid = $pr['petid']; $ptype = $pr['pname']; $im = $pr['pimage']; $number = $pr['number']; $limited = $pr['limited']; $types = $pr['types']; echo "<b>$ptype</b><br><a href=?petid=$pid><img src=/images/pets/$im></a><br>Number: $number<br><br>"; } } if(isset($getpetid)) { ?> <form action="<?php echo "$PHP_SELF"; ?>" method="POST"> Please select a Name: <input type="name" name="petname" maxlength="15"><br> Pet Gender: <select name="gender"><option value="female">Female</option><option value="male">Male</option><br><br><input type="submit" name="submit" value="Adopt Pet"></form> <?php $checkitq = mysql_query("SELECT * FROM userpets WHERE userpetname='$petname'"); $gcheckit = @mysql_num_rows($checkitq); $tq = "SELECT * FROM users WHERE username='$showusername'"; $t = mysql_query($tq); while($rw = mysql_fetch_array($t)) { $grabnum = $rw['pets']; } if(isset($adopt)) { if($grabnum >= "4") { echo "<font color=red>Error! You already have 4 pets.</font>"; } if($petname == "") { echo "<font color=red>Error! Please type in a name for your pet.</font>"; } if($qcheckit == "1") { echo "<font color=red>Error! That pet name already exists!</font>"; } if($gcheckit == "0" && $petname != "" && $grabnum < "4") { mysql_query("INSERT INTO userpets (owner, userpetname, userpetspecies, userpettype, petdatecreated, gender, strength, defence, health, level, booksread, smart, hunger) VALUES ('$showusername', '$petname', '$species', 'Normal', '$thedateis', '$petgen', '1', '1', '10', '1', '0', 'Normal', 'Full')"); mysql_QUERY("UPDATE users SET pets=pets+1 WHERE username='$showusername'"); echo "<font color=green>Success! Your pet has been created!</font>"; } } } } ?> </html> Thanks in advance for the help everyone!! i have to tables... forums & subscription i would like to insert a random character string into 'forums' to be used as a subscription id. and then have the same random string to be inserted into 'subscription' How can I check this string to see if it has a ~ character using php? Code: [Select] $string = 'userlocation~credentials~specialties'; Hey all, I'm looking to produce a random 10 character string like: abcABC0123 when the below method is called: <?php function randomize(){ $chars = explode(' ',(range("a","z"))) . explode(' ',(range("A","Z"))) . explode(' ',(range("0","9"))); $pass = ""; for($i = 0; $i < $len; $i += 1){ array_push($pass,$chars[rand(count($chars)-1)]); } return $pass; } var_dump(randomize(10)); ?> But the randomize method is not returning desired result. Any idea what I'm doing wrong? Thanks for response. |