PHP - Strpos() Won't Accept Whitespaces?
Hy ya'll!
I've been trying to programatically read a string, but with no success. Code: [Select] function get_between($input, $start, $end) { $substr = substr($input, strlen($start)+strpos($input, $start), (strlen($input) - strpos($input, $end))*(-1)); return $substr; } $string = 'open -greetings hello -bye seeya'; echo get_between($string, '-greetings ', ' -bye'); // Output: hello echo get_between($string, '-greetings ', ' -'); // Outputs nothing echo get_between($string, '-greetings ', ' '); // Outputs nothing } It turns out that I don't know if "-bye" will always follow the value of "-greetings", so I can't call everything in between like the first way even tough it works fine in this example. Given that those values will always be a single word, I can use a whitespace as a delimiter to the right, but for some reason it won't return anything whatsoever, not even an error code. Any ideas how to get it done? Txs a bunch! Similar TutorialsHi, I'll be honest don't have a clue what I am doing but a friend asked me to look into her site, she gets the following error at the top of this page http://www.morairaluxuryvillas.com/rentals/villalist.php Warning: strpos() [function.strpos]: Offset not contained in string in /home/morairal/public_html/rentals/villalist.php on line 85 the code on the line 85 is: $secondfstemp = strpos($block1, ".", $firstfs); I am more than willing to look into but don't know where to start - anyone have any ideas? Thanks for your time:-) Hi Guys I've been trying to work on my OS Commerce site, but for some reason I'm getting this error on the main index.php page. Welcome Guest! Would you like to log yourself in? Or would you prefer to create an account? Feel free to click any of the links below to access the main categories. Warning: strpos() [function.strpos]: Empty delimiter. in /var/www/vhosts/XXXXXX.co.uk/httpdocs/shop/index.php on line 319 319 shows this: if (isset($cPath) && strpos('_', $cPath)) { Any ideas where this error comes from? Ok, i've encountered the worse bug in my life. I worked over it for about 12 hours until I found the source, and as always, it is somthing quite stupid... after long research I found that the problem is that there are whitespaces in my DB... the thing is, that I can't get rid of them... I tried to use "trim" - like this: Code: [Select] $name = trim($name);and like this: Code: [Select] $name = trim($name, ' '); I tried: Code: [Select] if ($name[0] = ' ') $name[0] = '' but the little white spaces are still there. I thought it might be non-breaking space html char but when I looked at the source it was still a tiny space. any ideas? thanks When I checked it in phpMyAdmin I couldn't see it in the result list, but when I edited a row it was there and I was able to delete it... I'm not sure what else data is required but please ask anything you need because I'm running out of ideas Hi, Now I'm working with Joomla! site with virtuemart shop engine, and my problem is I sometimes find an annoying whitespaces. These whitespaces create an empty space that destruct the layout. I've manage to remove it with jquery trim. However, it must also be done with PHP because the script seems to consider this as not empty, so the modules, which suppose to be hidden if it's empty, is just displaying empty space. I've tried trim, but it's not working.. it's still showing empty string. Like the image bellow if I open the browser console (circled): If i copy the whitespace from the console and paste it to notepad, it shows me empty space and some question marks (?). Is there any advice how to get rid of this annoying problem? Thanks, ayok Code: [Select] <label for="first_name">* First Name </label> <input type="text" name="first_name" maxlength="64" value=<?php echo formatPhone(clean_input($_POST['first_name']); ?>> I have a form where I want to enter a first name. I want the fields to take only alphabets(no numbers). Also when a user enters something like John Doe separated by multiple white spaces, I want it to separate the two words only by a single space. I am not sure how this is done in php. I'm new in php and I don't have knowledge of php.
I just want to know, whenever we download file from server to Desktop through FTP, there are whitespaces occuring in files.
Like html websites, we always remove whitespaces whenever we upload the file to server after editing it. Is this step necessary for php websites also? If necessary then how to do it?
Hey guys I need some help I did a quick search and couldn't find my problem so I hope I didn't miss it. Anyways, I've got a bit of code in which when a specific website is visited, it generates a log. This is to track the number of times the website gets visited and from where. I know this sounds like a counter, but it's different. I'm having a problem though, because right now, I can't get it to generate for ONLY the website entered. It generates for EVERY website instead of just the specific one. Right now this is what I have.. if (strpos($_url, "MY_SPECIFIC_WEBSITE") //URL { I've tried adding to it to fix it, but I'm obviously still learning PHP and am sick of strugglnig. If anyone could please help I would appreciate it so much. Hello, Am a little puzzled on how to effectively determine if a character exists in a string. I am trying to come to grips with the function strpos but its a little confusing. If am searching for an underscore in a string and want to echo yes if the string contains an underscore, would the code below be right? Would it be right to exclude booleans TRUE and FALSE Example 1 Code: [Select] <?php $string = "Hello_World"; if(strpos($string, "_")) { echo "yes"; } OR do I have to include the booleans TRUE/FALSE Example 2 Code: [Select] <?php $string = "Hello_World"; if(strpos($string, "_") == TRUE) { echo "yes"; } How do I do two strpos conditions? here is kinda what I have.... if (strpos($val,'Sparring') && ($val,'Ecas') { } Hi Im having a problem finding the posistion of a div tag I tried this piece of code $fpos= strpos($snip, "<div id=\"footer\">") Then this piece $fpos= strpos($snip, "<div id="footer">") The $snip is the entire html document I thought maybe the \ might be causeing problem but without it Then i tried this any suggestions guys Thanks James This will be an easy one for you guys...
One of the fields in a MySQL database is a varchar() that was populated using a checkbox method and can contain more than one response. in this case i have blue, yellow, green and want to check if the field contains blue before echoing the statement to the screen. How would i use strpos in this case? ive tried a few thigns and get syntax error. The below of course works if the field ONLY contains blue. if (($row['colorsyoulike']) =='blue') { echo "<tr>"; echo "<td>Blue is an available Color!</td>"; } thanks. I have a string like this: Code: [Select] $data = "<a href="">Arbitrary characters. Bla bla bla</a><p>123456789</p><p>$10,100</p><p>01/02/2010</p><p>01/02/2011</p>"; What I am trying to accomplish is to extract the number between the first <p> tags as well as the date between the last <p> tags. I can extract the first number just fine by doing this: Code: [Select] $opening_p = strpos($data, '<p>'); $opening_p += 3; $closing_p = strpos($data, '</p>'); $length = $closing_p - $opening_p; // The length of the number changes, so I need to know the current length $first_number = substr($data, $opening_p, $length); However, no matter what I do, the position for the fourth <p> comes out to be the same as the first <p> In other words, this produces exactly the same number for the <p> Code: [Select] $opening_fourth_p = strpos($data, '<p>', 4); $opening_fourth_p += 3; So even with the offset in strpos, the position is exactly the same for both "extractions". So to conclude, both of these returns the same value: Code: [Select] $opening_p = strpos($data, '<p>'); $opening_fourth_p = strpos($data, '<p>', 4); Is there something I miss? Which is most efficient for finding a string within a string? Eregi or strpos? Currently im using Eregi to check a string again an array of blocked words and retrun true if one is present. But it seems i can do it also with strpos... so which is best function to use ? Great alliteration in the title. *Ahem* Anyway I have this code that I use for my chat system, And I'm making a command system. However when I send the command, It picks it up as a command etc. but it is so stubborn it will not realise it. I'm guessing it's my human error again like usual but take a look, See if you can help me (Please note the filea() function is for debugging it's telling me where the code (by printing to file) is taking me as the way I'm using it I can't just use echo or print whatever and see the results on a page) if(substr($text, 0, 2)=="::"){ //the command trigger is :: filea("Picked up the start of the command"); $subt1=substr($text, 2); //seperate the command trigger, In my case :: to execute the command. filea("Just checking the string... ".$subt1); $comms=array('ban', 'mute', 'cls', 'sysm', 'unmute'); $match=false; foreach($comms as $comm){ $commtest2=strpos($subt1, $comm); if($comtest2!=false){ //I also tried ==true first with no success $match=true; $command=$comm; break; } } filea("Ran command checker"); if($match==false){ filea("Command not found match=".$match); exit; //Exit out if there were no matching commands. } When I run this command: ::mute test this is the log that gets given to me, As you can see from the code this is what I put in there. Code: [Select] 3:07 AM) Logged in //this isn't in the code... (3:07 AM) Set text (3:07 AM) finding stats of person gergy008 (3:07 AM) Not muted. Continue... (3:07 AM) Has powers //up to here. But you don't need this. :) (3:07 AM) Picked up the start of the command (3:07 AM) Just checking the string... mute test (3:07 AM) Ran command checker (3:07 AM) Command not found match= As you can see I'm like -.- it's finding that the command is mute but it won't find mute in that string. It's dumb. Can someone help me find what the heck is wrong with this? I will be truely greatful Thanks! Hi all I am grabbing the _SERVER['REQUEST_URI'] and trying to compare it but this doesnt work $currpage = $_SERVER['REQUEST_URI'];//when i echo this i get ' / ' $home = '/'; //but this doesnt work if(strpos($currpage, $home)){echo 'test works';} PS It will work if i use if($currpage == $home){echo 'test2';} Any ideas? Thanks, Magnetica Code: [Select] $mystring = "|Signature"; $findme = '|'; $pos = strpos($mystring, $findme); echo $pos; echo's 0 supposed to echo out 1....? why not work thanks How can I enable it to check for Code: [Select] | using strpos? If $allcat=10 and $cat_id in my While loop is 1,2,4,5,6,7,8,10,12 Why does $pos = strpos($allcat,$cat_id); select 1 and 10? My script: Code: [Select] $ssql="SELECT * FROM adcat WHERE disabled='0' ORDER BY name ASC"; $sresult=mysql_query($ssql); while($srows = mysql_fetch_array($sresult)){ $cat_id = $srows['id']; $cat_name = $srows['name']; $link=$srows['clinkname']; $tot_ads = $srows['ads']; $cat_desc = $srows['description']; $rrows=$srows['div']; $catcol1=$srows['catcol1']; $catcol2=$srows['catcol2']; $catcol3=$srows['catcol3']; $catcol3a=$srows['catcol3a']; $divsub=$srows['divsub']; ///////////////// $pos = strpos($allcat,$cat_id); if($pos === false) { $selected[$cat_id]=''; }else { $selected[$cat_id]='selected="selected"'; } //////////////// echo '<option '.$selected[$cat_id].'>'.$cat_name.'</option>'; } ?> </select> Hi I have a page pulling information from mysql table, it lists names, I have these inside a hyperlink so that where there is a website listed for that record then the user can click on the name and move to their website. Only thing is that what happens if there is not a website for that record. I wanted a script so that if there is a website address listed then it prints the hyperlink but where there is not a website address listed that only the name is printed and it is not a hyperlink. I understand that the strpos can be used for this I have written a script but it is only printing out the name even when there is a website address in the row. What is wrong? PHP Version 5.3.4 Any help very much appreciated Code: [Select] <?php $website = $org_website; $link = 'http'; $weblink = strpos ($website,$link); if ($weblink === !false) { echo "<a href=\"$org_website;\"> FindOrganiserName($org_id); \"> FindOrganiserName($org_website); </a>"; } else { echo FindOrganiserName($org_id); } ?> I find it a bit buggy that strpos returns false instead of -1.
The equivalent C implementation returns -1:
int strpos(char s[], char t[]) { int i, j, k; for (i = 0; s[i] != '\0'; i++) { for (j=i, k=0; t[k]!='\0' && s[j]==t[k]; j++, k++) ; if (k > 0 && t[k] == '\0') return i; } return -1; }The problem with returning false is that false can also evaluate to 0 which can also happen to be the first index of a match. Therefore, you have a hard to find bug. I know that you can use the identity operator to check for falseness: ===. But wouldn't it be a better design for the function to just return -1? Hello everyone, When someone starts an OC (Organised Crime) On my game, they invite 3 people. I was just testing it and found that everything is fine up until 1 point, the actual Inbox mail that the invited user gets. I can't click Accept or Reject? Here's the bit of code i have... Code: [Select] if (strip_tags($_POST['inv']) == "inv_we"){ $inv_username=strip_tags($_POST['inv_username']); $check = mysql_num_rows(mysql_query("SELECT * FROM users WHERE username='$inv_username'")); if ($check == "0"){ echo "No such user."; }elseif ($check != "0"){ if ($oc->we != "0"){ echo "You need to kick the Weapons expert before you invite someone else."; }elseif ($oc->we == "0"){ $invite_text=" <div align=center>You have been invited to join $username's Organised crime as the Weapons Expert, please choose one of the following options:<br> <input name=Decline type=submit id=Decline class=button value=Decline> | <input name=inv_button type=submit class=button id=inv_button value=Accept> </div><input type=hidden name=place value=we><input type=hidden name=oc_id value=$oc->id> "; mysql_query("INSERT INTO `inbox` ( `id` , `to` , `from` , `message` , `date` , `read` ) VALUES ( '', '$inv_username', '$inv_username', '$invite_text', '$date', '0' )"); echo "Weapons expert invited"; mysql_query("UPDATE oc SET we_inv='$inv_username' WHERE id='$oc->id'"); |