PHP - Get String Before Line Break
How do i get the first part of the string, all the way up until the string hits a line break?
Code: [Select] <?php $string = "The quick red fox\n jumped over the brown\n lazy dog."; function first_of_string($str) { //php magic return $first_of_string; } echo first_of_string($string); //would produce "The quick red fox" ?> Similar TutorialsHello, I have this XML file I am parsing and I have hit a small formatting issue that I can't seem to get around.
Here is what the the output of the RSS looks like.
<description><![CDATA[<div style='text-align:left;'>Exit ramp closed. <br/><b>Current Status:</b> Open<br/><b>Affected Lanes:</b> Exit Ramp<br/><b>Dates:</b> Wednesday, May 28 - Thursday, May 29<br/><b>Days Closed:</b> <font color='#808080'>S</font><font color='#808080'>M</font><font color='#808080'>T</font><font color='#FF0000'><b>W</b></font><font color='#FF0000'><b>T</b></font><font color='#808080'>F</font><font color='#808080'>S</font> 20:00 PM - 6:00 AM</div>]]></description>Notice at the first is "Exit ramp closed." There is nothing before it but, for some weird reason when I parse it. It is on a new line like so, notice it should be up there with "Incident" but, it is dropped down below it. Incident: Exit ramp closed. Current Status: Open Affected Lanes: Exit Ramp Dates: Wednesday, May 28 - Thursday, May 29 Days Closed: SMTWTFS 20:00 PM - 6:00 AMThis is how it is formated in the td cell. <td style='{$td2Style}'><strong>Incident:</strong> {$incident_data_desc}</td>\n";So as you can see above, there is nothing before it that would cause it to break to a new line both in the table or in the RSS. So how can I remove that first return only is that is on the same line with it and not below it. I have tried this and a couple of other things like trim with no luck. $description = $item->description; $incident_data_desc = str_replace("\r", '', $description );Note, I only need that first one removed, the others are fine. I can do it but it will remove all the breaks and I only want to remove that first one that is causing the line to be on a new line. Any suggestions? -Thanks echo '<div class="gamename">'.$values[0].'</div>'; echo '<div class="gameimage"><a href="'.$values[3].'"><img src="'.$values[1].'"></a></div>'; NOW i want 2nd echo under 1st echo. I tried every single <br> combination but no luck Also my 2nd question is: my image and link is a variable, it displays the image but there is something wrong with link. Variable is just like this www.mydomain.com\image\blabla.php but it shows link as==> www.mydomain.com\ instead of what i wrote above. Code: [Select] $result = mysql_query("SHOW COLUMNS FROM ".$table.""); $i = 0; if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_assoc($result)) { $csv_output .= $row['Field'].","; $i++; } } $csv_output .= "\n"; $values = mysql_query("SELECT * FROM ".$table.""); while ($rowr = mysql_fetch_row($values)) { for ($j=0;$j<$i;$j++) { $csv_output .= $rowr[$j].", "; } $csv_output .= "\n"; } print $csv_output; I'm using the above to get a csv output in my browser, but it appears as a string of text eg a1,a2,a3,a4,b1,b2,b3,b4,... I'd like it to appear as a1,a2,a3,a4 b1,b2,b3,b4 c1,c2,c3,c4 Any ideas how? hi i have some troubles to create a new line break for this code: i already tried: Code: [Select] echo "<input type='radio' name='gropu1' value='$id'>$data \n "; // Code: [Select] echo "<input type='radio' name='gropu1' value='$id'>$data.\n "; // Code: [Select] echo "<input type='radio' name='gropu1' value='$id' >$data.'\n'"; what is the correct syntax for that? I want to have a line break between the lastname and the age. Please help. echo $line["firstname"].' '.$line["lastname"].' Age is'.$line["age"].'!'; I'm trying to use SESSIONS, the start of my page looks like this: <?php session_start(); and the start of the source code looks like this: <br /> <b>Warning</b>: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cookie - headers already sent by The <?php tag is on line 1, yet as you can see Notepad++ seems to add a <br /> before it, which results in the sessions not working. How can I fix this? I'm trying to figure out how I can break a string down if the strings length is longer then lets say 140 characters including white space. At first I was figuring maybe maybe I could do something with explode or implode after I get a get my strings length but the string in question has no static values to them that would let me break it apart or put it back together. Now In most cases I would use substr() to return my 140 characters, however I need to take 140+ and make an array of the extra so I can run a for each on it. I am using this for an SMS app I am helping develop. I know I can limit my the output to only 140, just based on the notion that a single SMS is capped at 140-160 on most carriers. Unfortunately for me thats not the case so I need to break this apart and I am stumped, anyone got any ideas? I am extracting data from one of the sites.I would join these sentences separated by , (comma). The solution that comes to my head is by inserting the text in the file and then using file functions.However, i was wondering if i can use str_replace here . Can anyone please point me in the right direction. Code: [Select] Hospital 30-Day Death (Mortality) Rates from Heart Attack: 15.2% (No different than U.S. National Rate) Hospital 30-Day Readmission Rates from Heart Attack: % (Number of Cases Too Small*) Hospital 30-Day Death (Mortality) Rates from Heart Failu 10.5% (No different than U.S. National Rate) Hospital 30-Day Readmission Rates from Heart Failu 29.9% (Worse than U.S. National Rate) Hospital 30-Day Death (Mortality) Rates from Pneumonia: 9.8% (No different than U.S. National Rate) Hospital 30-Day Readmission Rates from Pneumonia: 20.6% (No different than U.S. National Rate) hi, i am using a database to populate a webpage with text. How do I make a function to add line breaks to the text? an example page I want to use it on it http://www.stayneartheairport.com/bandb.php?id=11084507&airport=manchester You can see the two text areas of description and facilities have no paragraphs or other formatting. I want the code to scan the text - and if there is say <br> - it will remove it an replace it with a line break. so i have this string that i want to search on the database My database has 3 parts city, state, country and i have a string that is say "new york city, ny, united states" and i want to be able to be able to look into the database but search those three sections so for example "New York City" i would like to search under city "NY" i want it to search under state and "United States" i want to search under country so basically how can i split the string into those three sections?? hope someone can help me with this how do I make this form here have a limit on the value's name, then it moves to the next line? Code: [Select] <form action=\"reply.php?com_id=$com_id\" method=\"post\"> <font color='brown'><u><input type=\"submit\" name ='submit' class=\"link\" value='$title'></u></font> </form> The table in the script has a set max-width. How can now a too long continuous text string as in "blaaaaaaaa", which also does not contain a space or dash, be broken, so the table does not become stretched to far? Which function is used for this? I am having problems with these slashes. They keep showing up when I echo data.... The code is to update a field in a mysql database table. Here is my code: Code: [Select] $query = mysql_query("SELECT * FROM `bus` WHERE username='$username'"); $row = mysql_fetch_array($query); $bio = $row['about']; $bio = str_replace("<br />","\r\n",$bio); if(isset($_POST['submit'])){ if(empty($errors)){ if($password == $pass){ $about1 = mysql_real_escape_string($_POST["about"]); $about1 = str_replace("\r\n","<br />",$about1); mysql_query("UPDATE bus SET about='$about1' WHERE username='$username'"); }else{$errors[] = 'Incorrect Password';} } } When I run this and put: About: I'm very tired today It's been very hard I get the about1 string as: I\\\'m very tired today\r\nIt\\\'s been very hard Why??? 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. Hi to everyone. So, I have a little php code that reads line by line from a txt file, specifically i have 1 IP address for each line, and compares it with the current user's IP. It's like a little blacklist code. But it doesn't work. Can anyone see what i am doing wrong? Code: [Select] $file = fopen("ipblacklist.txt", "r") or exit("Unable to open file!"); //Output a line of the file until the end is reached $ipblacklist=''; $refile=''; while(!feof($file)) { if (fgets($file)== $_SERVER['REMOTE_ADDR']) $ipblacklist='error'; $refile=fgets($file); } fclose($file); Thank you in advance Hey, all. I'm new to php/mysql. I'm getting the warning: Quote mysql_fetch_array() expects parameter 1 to be resource, string...line 39 with reference to the mysql_fetch_array function in the following code: mysql_select_db("calculators", $con); $query = "SELECT * FROM calculator WHERE Abbreviation = '" . $_GET['abbreviation'] . "'"; $result = mysql_query($query, $con); while($row = mysql_fetch_array($result)) { //some code... } The code runs fine, but I'd like to know why I'm getting the warning. I've looked at similar post on this forum and other forums and can't quite get a straight answer. Thanks a bunch, Davis I am having more trouble with my code, please see the error below when loading my browser: Here is my blog Fatal error: Function name must be a string in W:\www\blog\index.php on line 14 My code is: Code: [Select] <?php mysql_connect ("localhost", "root", "gwalia"); mysql_select_db("blog"); ?> <html> <head> <title>Show My Blog</title> </head> <body> Here is my blog<hr/> <?php $sql = mysql_query("SELECT * FROM blogdata ORDER BY id DESC"); While($row = mysql_fetch_array($sql)){ $title = $row('title'); $content = $row('content'); $category = $row('category'); ?> <table border='1'> <tr><td><?php echo $title; ?></td><td><?php echo $category; ?></td></tr> <tr><td colspan='2'><?php echo $content; ?></td><td></tr> </table> <?php } ?> </body> </html> Line 14 is '$title = $row('title');'. But i do not know what is wrong with my code, help please? what im trying to do is take a youtube embed code find the URL code for that video and remove all other parts of the code keeping only the URL of the video after i get the URL by it self then replace the http://www.youtube.com/ part of the URL with http://i2.ytimg.com/vi/ to do this i know that i need something like this to get the URL of the video http://www.youtube.com/((?:v|cp)/[A-Za-z0-9\-_=]+) but how to only return just the URL is something idk how to do then use str_replace http://www.youtube.com/(?:v|cp)/" with http://i2.ytimg.com/vi/ so in the end im asking if anyone know how to remove all other codes i dont want and only keep the URL this may have to be done using "regex" im not sure as i dont know to much about regex and what it can do but does sound like it would help lol Hi. I want a simple textbox, that when submited, will replace very every new line, with the <br> tag. What will happen, when it submits, it will take the contents of a textbox, add the <br> tag where a new line is suposed to be, and save the string to a MySQL Database. I think this is the easiest way of allowing a user to edit what appears on a website when logged in, but if there is a easier way, then please tell me. What I am trying to do, is a login page, and once logged in, you enter new text into the textbox, click submit, and on the website homepage, the main text will change to what was submitted. But if there is a new line, I think the only way in HTML to make one is to put a <br> tag, so I want the PHP to but that tag wherever there is a new line. Sorry if I am confusing, I am not that advanced at PHP, but I would be very happy if you could supply me with the correct code. Time is running out... If you do not understand me, please tell me -- PHPLeader (not) Dear All Good Day, i am new to PHP (a beautiful server side scripting language). i want to send a mail with line by line message i tried with different types like by placing the things in table and using <br /> but the thing is the tags are also visible in the message of the mail. Here is my code: $message1 = "Name :". $_REQUEST['name']."<br />"; $message1 .= "Surname :". $_REQUEST['surname']."<br />"; $message1 .= "Cellphone :". $_REQUEST['mobileno']."<br />"; $message1 .= "Telephone :". $_REQUEST['landno']."<br />"; $message1 .= "Fax :". $_REQUEST['fax']."<br />"; $message1 .= "Company :". $_REQUEST['company']."<br />"; $message1 .= "Email :". $_REQUEST['email']."<br />"; $message1 .= "Country :". $_REQUEST['country']."<br />"; $message1 .= "Enquity :". $_REQUEST['enquiry']."<br />"; $message1 .= "Date&Time :". $date."<br />"; For this code if try to print/echo it it is working fine it is displaying line by line, but using this variable ($message1) in the mail these <br /> are also visible. Can any one guide me to resolve(to remove these tags from the message part) this issue. Thanks in Advance. :confused: |