PHP - How Can I Concat Two String Removing Newline Characters From Mddle Of The String
Similar TutorialsHello Everyone, I am working on a project I pull the field names from the database. I am trying to remove the underscore (_) when the field name is displayed. Does anyone know how I would do that? Here is the code that I am working with Code: [Select] $i = 0; while ($i < mysql_num_fields($fieldnamesquery_result)) { $meta = mysql_fetch_field($fieldnamesquery_result, $i); if (!$meta) { echo "No information available"; } $meta = str_replace("_", " ", $meta); $tablecolumn .= "<p>" . $meta->name . ":<input type='text' name='" . $meta->name . "' id='" . $meta->name . "' /></p>"; $i++; In <div id="chart_rt">%3$s</div> below, it is populated with a string that always begins with a number followed by a colon (from 1: to 25:). I see that ltrim removes characters from the beginning of a string but I don't see how to compensate for the extra character in 10: through 25: function wp_rss( $url, $num_items = -1 ) { if ( $rss = fetch_rss( $url ) ) { echo '<div id="charts">'; if ( $num_items !== -1 ) { $rss->items = array_slice( $rss->items, 0, $num_items ); } //start count $i = 1; foreach ( (array) $rss->items as $item ) { printf( '<div id="chart_lt">'.$i.'</div><div id="chart_rt">%3$s</div>', esc_url( $item['link'] ), esc_attr( strip_tags( $item['description'] ) ), htmlentities( $item['title'] ) ); //increment $i++; } echo '</div>'; } else { _e( 'An error has occurred, which probably means the feed is down. Try again later.' ); } } I have a string of mixed Japanese or Chinese and Western characters and I would really like to leave the western characters and remove the others. Is there any way to detect which UTF-8 characters are Japanese/Chinese and to remove them? I have tried to use this which I found on another thread but my version of PHP (5.3) will not allow the /u in the following Code: [Select] echo "<BR>".preg_replace('/[^\u4E00-\u9FFF]+/', '', $string); I have tried using various combinations of /p (these, plus others) Code: [Select] echo "<BR>".preg_replace('/\p{Bopomofo}+/u', '', $string); echo "<BR>".preg_replace('/\p{Hiragana}+/u', '', $string); and the /x{} option tells me the numbers are to large Code: [Select] echo "<BR>".preg_replace('/[^\x{4E00}-\x{9FFF}]+/', '', $string); My string for this example is this but any combination of western and Japanese/Chinese characters are possible in the title. 2011.12.06 19:00-20:00 / ふくいちライブカメラ (Live Fukushima Nuclear Plant Cam) | Uploaded: 06 Dec 2011 Thanks for any help. I have the following function, which takes a string with commas in it and attempts remove those commas. The way I have it here is that I use explode to take out the commas, which makes an array, and then iterate through that array to put a string back together without the commas. function tags_to_sort_by( $sortMeta ) { $sortByArray = explode(",", $sortMeta); $sortByCount = count($sortByArray); for ($i = 0; $i < $sortByCount; $i++) { $sortByString += $sortByArray[$i]; } return $sortByString; } What does not work is the following line: $sortByString += $sortByArray[$i]; Somehow that outputs a 0, which I don't understand. It should output something like: arrayItem1 arrayItem2 array3 etc. My question is if there either is an easier way to remove the commas from the original string or what I am doing wrong in this line: $sortByString += $sortByArray[$i]; // I am trying to concatenate each part of the array back into the string. Thanks a lot for help with this! What I want to do is simple: The string "this_is_an_example.php" should return "this_is_an_" while: "this_is_an_example_filename.php should return "this_is_an_example_" Is there a simple function which can cut off the part after the last _ ? I have been looking and trying several functions, but can't seem to find the right one for this. (the last _ being cut off of course wouldn't be a problem I couldn't live with :-) ) Any help will be greatly appreciated! hi im having trouble removing ' from strings
so say
$name = "D'vion";
$newname = str_replace("'","",$name);
but this just returns the same.
thanks matt
Hey all, I'm using the form 'method="get" action="results.php"' on my search form... Say someone uses my 'Photo search' to search my database for aircraft, and leaves all fields blank. In this case, the only field of the search is 'aircraft'. This will take the user to 'results.php?aircraft='. Is there anyway to remove the sting if there is no search value? So it would just read 'results.php'? This is the code i have so far: Code: [Select] <?php if(isset($_GET['aircraft'])){ $aircraft = $_GET['aircraft']; } if($aircraft==""){ $aircraft = mysql_query("IGNORE aircraft"); } ?> Thanks in advanced! Hello guys, I want to remove part of a string and convert it to a var instead of echoing it out.. How would I do it.. $string = "temp_photo/testing.jpg"; I want to remove temp_photo so the out put should be $filename2 = testing.jpg Please advise.. Thanks, Dan Does anybody know of a function or a way of letting me get the string between whatever characters? Say I had [SOMEWORD] text [SOMEWORD] then how could I go about getting the value "text", please note I'm not trying to make bbcode or similar. Hey guys! I found a flush function and it seems to be working well, but sometimes it will cut off characters. It's very strange. Here's my flush function: function out($str) { echo $str."<br />\n"; vbflush(); } function vbflush() { if (ob_get_length()){ @ob_flush(); @flush(); @ob_end_flush(); } @ob_start(); }And I'm calling it like this:out('Waiting 30 seconds until execution');And this is my output: Code: [Select] Waiting 30 seconds until executioAnyone know why? %3$s displays a string of text that always begins with a number followed by a colon (from 1-25 only). 1: 2: 3: etc... Is there a way to remove the number and the colon entirely and leave the rest of the string intact? Code: [Select] function wp_rss( $url, $num_items = -1 ) { if ( $rss = fetch_rss( $url ) ) { if ( $num_items !== -1 ) { $rss->items = array_slice( $rss->items, 0, $num_items ); } //start count $i = 1; foreach ( (array) $rss->items as $item ) { printf( '<div title="%2$s"><strong>'.$i.'</strong></div><div>%3$s</div>', esc_url( $item['link'] ), esc_attr( strip_tags( $item['description'] ) ), htmlentities( $item['title'] ) ); //increment $i++; } Ashley How I can explode by a series of characters (not one single character)? For example, divide the string to an array by any of these characters "," and ";" and "." $string = "this, that;some.word;second"; I want to put all of these words into an array. Hi, I am trying to search a string for a string of characters in this order ../ and carry out relevant if statements but it returns the same result every time. On echoing my $pos, $pos2, $pos3 and $pos4 variables the first two come back 0 and they are the strings that do contain ../ but the if statements perform the same? I don't understand, any ideas? Thanks... Code: [Select] <?php $logo = $info ['logo']; $pos = strpos($logo, "../");//check if image contains ../ if ($pos == 0) {} else { $logo = substr($logo, 3);} $image1 = $info ['image1']; $pos1 = strpos($image1, "../");//check if image contains ../ if ($pos1 == 0) {} else { $image1 = substr($image1, 3);} $image2 = $info ['image2']; $pos2 = strpos($image2, "../");//check if image contains ../ if ($pos2 == 0) {} else { $image2 = substr($image2, 3);} $image3 = $info ['image3']; $pos3 = strpos($image3, "../"); //check if image contains ../ if ($pos3 == 0) {} else { $image3 = substr($image3, 3);} ?> Am looking forward to removing all the numeric characters upto and including the leading "/" in 90/_featuredarticles/2011/12 Regards This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=321479.0 Hello, 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 Hello, Any help would be great. I'm filtering and rss feed and to create a new feed with some <item>'s removed. Here is a snippet from the source file. <item> <title><h1>Indians, Pacers and Colts attendance is only part of story</h1></title> <link>http://www.newsweek.com/the-score/2010/07/29/indians-pacers-and-colts-draw-similar-sized-crowds/PARAMS/post/21385</link> <description></description> </item> <item> <title><h1>Declining attendance forcing Speedway to make changes</h1></title> <link>http://www.newsweek.com/declining-attendance-forcing-speedway-to-make-changes/PARAMS/article/21369</link> <description></description> </item> <item> <title><h1>Three issues that could derail Indy's 2012 Super Bowl</h1></title> <link>http://www.newsweek.com/the-score/2010/07/27/three-issues-that-could-derail-indys-2012-super-bowl/PARAMS/post/21341</link> <description></description> </item> I'm wanting to use the link tag to detect the item that need to be removed for the new rss feed. All <item>with <link> that contain http://www.newsweek.com/the-score/ as the beginning of the link need to be filtered out. Here is my code so far. <?php echo '<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:media = "http://search.yahoo.com/mrss"> <channel> <title>CLE Calendar</title> <description>CLE Calendar</description> <language>en-us</language> <pubDate>June 14, 2010</pubDate> <lastBuildDate>June 14, 2010</lastBuildDate> <ttl>15</ttl>'; ?> <?php $rss = new SimpleXMLElement('http://www.newsweek.com/mobile-app/rss/feed?rssId=18&premium=false', null, true); ?> <?php foreach ($rss->channel->item as $items): ?> <item> <title><?php echo '<h1>' . $items->title . '</h1>'; ?></title> <link><?php echo $items->link; ?></link> </item> <?php endforeach; ?> </channel> </rss> Thanks in advance, MK Hi I am trying to replace characters in a string with images so character 'a' will be replaced with a.jpg etc I have tried the following code but that doesn't result in the desired effect as the updated string gets it's characters replaced as the script works through it so the 'img' in the first updated string gets replaced with with images and it all ends up quite a mess. Is there a simpler way to do this? $content = "abc" ; $content = str_replace("a", "<img src=images/a.jpg >", $content); $content = str_replace("b", "<img src=images/b.jpg >", $content); $content = str_replace("c", "<img src=images/c.jpg >", $content); $content = str_replace("d", "<img src=images/d.jpg >", $content); $content = str_replace("e", "<img src=images/e.jpg >", $content); $content = str_replace("f", "<img src=images/f.jpg >", $content); $content = str_replace("g", "<img src=images/g.jpg >", $content); $content = str_replace("h", "<img src=images/h.jpg >", $content); $content = str_replace("i", "<img src=images/i.jpg >", $content); $content = str_replace("j", "<img src=images/j.jpg >", $content); $content = str_replace("k", "<img src=images/k.jpg >", $content); $content = str_replace("l", "<img src=images/l.jpg >", $content); $content = str_replace("m", "<img src=images/m.jpg >", $content); $content = str_replace("n", "<img src=images/n.jpg >", $content); $content = str_replace("o", "<img src=images/o.jpg >", $content); $content = str_replace("p", "<img src=images/p.jpg >", $content); $content = str_replace("q", "<img src=images/q.jpg >", $content); $content = str_replace("r", "<img src=images/r.jpg >", $content); $content = str_replace("s", "<img src=images/s.jpg >", $content); $content = str_replace("t", "<img src=images/t.jpg >", $content); $content = str_replace("u", "<img src=images/u.jpg >", $content); $content = str_replace("v", "<img src=images/v.jpg >", $content); $content = str_replace("w", "<img src=images/w.jpg >", $content); $content = str_replace("x", "<img src=images/x.jpg >", $content); $content = str_replace("y", "<img src=images/y.jpg >", $content); $content = str_replace("z", "<img src=images/z.jpg >", $content); echo $content ; Any help would be gratefully appreciated. Thanks Megalos (paul) I want to allow Alphabetical strings, Numeric strings, and Alphanumeric strings. For example "Bob", "1234", "bob1234" are all valid. I want to check if the string has invalid characters, return true if it does (or false, which ever method works). How can I do it? This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=320682.0 |