PHP - Trimming Help Php! Please Take A Look!
Hi Guys,
I have a search box on the site I am creating and when a user types their postcode in it the database is searched and members covering that location are shown.
I have the following which works fine ... but:
---------------------------------------------------------------------------------------------------------------
$postcode = mysqli_real_escape_string($myConnect, $_POST['searchbox']);
Similar TutorialsHi I have a recordset from a database which outputs a value of '% THOBEKA MFEKA::12110235675?' I need to seach the database based on the number between the :: and ? only. So for example '12110235675'. I have tried this with a sql statement using LIKE but its not working. How can I trim the value from the database so I only get the number out. The amount of characters before the :: can vary. Please can someone give me some advice asap. Rob This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=351556.0 I'm using the following code to attempt to strip white space from my search results: Code: [Select] while($row = mysql_fetch_assoc($rs)) { trim($row['Name']); trim($row['Address']); trim($row['City']); trim($row['State']); trim($row['Zip']); trim($row['Phone']); echo "<td>" . $row['Name'] . "</td>"; echo "<td>" . $row['Address'] . "</td>"; echo "<td>" . $row['City'] . "</td>"; echo "<td>" . $row['State'] . "</td>"; echo "<td>" . $row['Zip'] . "</td>"; echo "<td>" . $row['Phone'] . "</td>"; echo "<td>" . $row['Type'] . "</td>"; Obviously, I'm leaving out a ton of code, but the important part is that it's not stripping the white space. Things like this make me want to pound spikes into my eyes, because there's just no logical way for me to work though this. I'm extremely OCD, and so if code doesn't work the way the documentation states, I freeze. I also tried (as per another tutorial example) setting a second variable trimming it, and using the second variable in the Echo, like so: Code: [Select] $Tname = ($row['Name']); echo "Blah blah blah" . trim($Tname); That didn't work either. It's simply NOT removing the white space, despite the fact that the entire description of what "Trim" does consists of "removes white space." It boggles my mind. Am I supposed to sacrifice a goat first or something? BTW, if I go in and edit the database manually, I can strip out the spaces by hand, and everything works fine. But it seems to me like the Trim command is supposed to exist to make it so that I don't have to do that. The only thing I can guess is that for some reason the contents of the DB aren't being seen as Strings, even though they are, in fact, Strings. I'm able to maniupulate them as Strings everywhere else in the code. Just not here. Does anyone know what I did wrong? Thanks! Kyle I'm more or less a noob. I have used the below code on another webpage, but on a new site that I am working on I attempted to trim the variable and now the page won't load. Can someone show me what I did wrong or a better way to accomplish this? Code: [Select] <?php # GRAB THE VARIABLES FROM THE URL $x = $_GET['x']; $valid = array('cat:1', 'cat:2', 'cat:3', 'cat:4', 'cat:5', 'cat:6', 'cat:7', 'cat:8', 'cat:9', 'cat:10', 'cat:11'); if(in_array($x, $valid)) $trimmed = trim($x, "cat:"); include_once $trimmed . '.inc'; else include '1.inc'; ?> When I used this before my variables did not have a colon in them, so I did not have to trim the variable, thus the last part of the script was: Code: [Select] if(in_array($x, $valid)) include_once $x . '.inc'; else include('1.inc'); ?> Heres the code of the line im trying to trim
$r = mysql_query("insert into points values ('','${v[0]}', ${v[1]}, '$At')")or die (mysql_error()); mysql_query("insert ignore into toc values ('','$p1', '$At'),('','$p2', '$At'), ('','$p3', '$At'), ('','$p23', '$At')") or die (mysql_error()); if (!$r) die (mysql_error());the line pertaining to the question is the insert into points . what that part of the code does is takes this preformatted text START POINTS MrsSpacely 1 nanatravels 1 Trust_Me_honey 2 moratala 3 xxBIGDAVExx 3 Spring_Chicken 5 tomrarose 5 sitdownnplay 7 _Antoinette_ 11 Melyn 25 mokan40 30 dj_lia 10 STOP POINTS And inserts it into the points table. Which works fine but if you put a space after Stop points or before any name it comes back with an error about check spaces, and doesnt post the whole thing what i need to do is trim all the spaces from the text before it tries to go into the table Code: [Select] <? $out = preg_replace('/^(.{701}[^.]*).*/i','$1.',$detrsltnewsrow[news_desc]); echo $out; ?> </td></tr><tr><td colspan="2" class="para" style="padding-left:10px;"> <?= substr(stripslashes(trim($detrsltnewsrow[news_desc])),701) ?> </td></tr> I have the above snippet.. The first php statement, basically grasp the first 701 characters with the closet next stop "." character and out puts it. then out puts the HTML tags I have a problem with the second statement. I want to output anything after what has been outputted by: Code: [Select] <? $out = preg_replace('/^(.{701}[^.]*).*/i','$1.',$detrsltnewsrow[news_desc]); echo $out; ?> So need the correct syntax for Code: [Select] <?= substr(stripslashes(trim($detrsltnewsrow[news_desc])),701) ?> Currently it breaks at exactly the 701 character, want it to continue from the sentence the first code ended in. |