PHP - Simple Strpos Problem
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 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? 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> 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. 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! 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? 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); } ?> 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! 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? 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? It shouldn't be that difficult I just don't know how to do it myself. I'm using the following code for a simple navigation, i has been placed in the content of my home page, it works fine however i don't know how to set the home page. <div id="content"> <?php $page = $_GET['page']; $file = $page.".php"; if(file_exists($file)) { include($file); } else { include "error.php"; } ?> </div> so I have an xml string the following expression $xpath = $response->xpath('/atom:feed/atom:entry/atom:content'); returns the following array SimpleXMLElement Object ( [@attributes] => Array ( [type] => application/vnd.google-earth.kml+xml ) [Placemark] => SimpleXMLElement Object ( [name] => M1 [description] => SimpleXMLElement Object ( ) [Style] => SimpleXMLElement Object ( [IconStyle] => SimpleXMLElement Object ( [Icon] => SimpleXMLElement Object ( [href] => http://maps.gstatic.com/intl/en_ALL/mapfiles/ms/micons/blue-dot.png ) ) ) [Point] => SimpleXMLElement Object ( [coordinates] => -79.395018,43.645423,0.0 ) ) ) However what I'm trying to get are the coordinates, but neither $xpath = $response->xpath('/atom:feed/atom:entry/atom:content/Placemark'); OR $xpath = $response->xpath('/atom:feed/atom:entry/atom:content/Placemark/child::*'); work I even tried just loading atom:content into an array and going from there no luck. Any ideas. Oh. BTW this might help http://code.google.com/apis/maps/documentation/mapsdata/developers_guide_protocol.html#RetrievingFeatures SOLVED, sorry. ... but i can't work it out. all i want to do is; if ( $this_item exists within the table ) { do a } else { do b } for example, $this_item = 'john', and i want the script to do (a) if he's in the table, or (b) if he isn't. thank you! This is my website: www.wearenip.com/home.html This is the menu that should appear on the left hand side, but doesn't: www.wearenip.com/menu.php This is my server's output saying that php is running just fine: http://wearenip.com/phpinfo.php I'm using this code in the left side bar div: Code: [Select] <?php include("menu.php"); ?> The PHP file will not display from the HTML file no matter what I do. I've tried using every possible combination of file linking including /menu.php ./menu.php and even the full http://www.wearenip.com/menu.php etc etc etc I'm genuinely upset about this. Any help would be greatly appreciated. PS: I can't get any PHP file to properly include in any HTML file on any computer I have. Nothing displays whatsoever regardless of what is in the PHP file, even if it's just a text word. Doesn't matter if it's hosted on a PHP compatible internet server or just on a local server or just on the local machine itself. Nothing. heres the code... <?php mysql_connect("localhost","peter","tizer") or die("Error: ".mysqlerror()); mysql_select_db("matquant"); //replace TestTable with the name of your table //also in a real app you would get the id dynamically $idtochange=$_POST['idtochange']; $sql="select * from `quantites` where id = $idtochange"; $query=mysql_query($sql); while ($row=mysql_fetch_array($query)){ $id = $row['id']; $material = $row['material']; $quantity = $row['quantity']; $unit = $row['unit']; //we will echo these into the proper fields } mysql_free_result($query); ?> <html> <head> <title>Edit material list</title> <link href="css/quant1.css" rel="stylesheet" type="text/css"> </head> <body> <div id="leftcol"> <form "method="post"> <p><br/> <input type="text"value="1" name="idtochange" > <p> <input type="submit"value="submit changes"/> </form> <form action="changequan.php"method="post"> <p>Material Id currently active<br/> <input type="text"value="<?php echo $id;?>" name="id" disabled/> </p> <p> Enter new material name: </p> <input type="text"value="<?php echo $material;?>" name="material"/> <br/> change quantity: <input type="text"value="<?php echo $quantity;?>" name="quantity"/> <br/> unit of mesurement for quantity<br/> <input type="text"value="<?php echo $unit;?>" name="unit"/> <br/> </p> <input type="submit"value="submit changes"/> </form> </div> <div id="rightcol"><?php // make connection mysql_connect ("localhost", "peter", "tizer") or die ('I cannot connect to the database becuase: ' . mysql_error()); mysql_select_db ("matquant"); // build query $query = mysql_query("SELECT * FROM `quantites` ORDER BY `id` ASC"); // display results while ($row = mysql_fetch_array($query)) { echo "<br /> ..... " .$row ['id']. " ....... " .$row ['material']. " ...... " .$row['quantity']. " ...... " .$row['unit']. "<br />";} echo mysql_error(); ?> <a href="index.html">Home </a> <p> <a href="quanform.php">Insert new material and quantity form</a></p></div> </body> </html> I'm struggling with php. Any help anyone can give would be appreciated. |