PHP - Split A String At A Certain Number Of Characters But Not In The Middle Of A Word
How could I split a string in half but not in the middle of a word?
$information = "This is the string of text, however at some point in the sentence a word may get split in half."; $cut = round(strlen($information)/2); ///where to cut (50%) $split1 = substr($information, 0, $cut); //first half $split2 = substr($information, $cut); // second half Similar TutorialsI have a large amount of text, (text can be different at any time), I count the number of characters in the string,and divide it by 2 so that half of the text will be in one variable, the other half in the other, however I am not sure how to get the split function, or whichever function to split it by the number of characters. example: $text = "lorem ispum dolar sit amet, consecterur adiscing elit. Donec dictum placerat sapien, in eliefend liber"; $cut = strlen($text)/2; //(here's where i cannot get it to work/do not know which function to use and how to use it) //but what I want it to do is to split it half way. This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=322184.0 Hi, I have a number (int) stored in the database in this case a uk Telephone Number, its stored as like 01616276576 what i want to be able to do is pull it out of db and split it into chucks like 0161 627 6576 how can i do this? <?php $title = "This is an example of a sentence in a paragraph"; $title_array = explode(" ", $title); // splits the string by string $chr_count = array(); foreach($title_array AS $word){ $chr_count[] = strlen($word); } echo $chr_count[0]; $count = count($title_array); for($i=0; $i<$count; $i++) { ??????? } ?> i want to store the count for every word in the $title, in a variable, but i get an error. Actually i need the count of every word so that i can store every word which has greater than 5 characters in an array variable! Then i would use this variable to compare words in another paragraph to check whether sentences in the other paragraph contain these words. If they contain these words then i would add the sentence to a variable $summary which would make the summary of the original paragraph. Any ideas please? I need to split a string, but in a very unusual way. $string1 = "HelloWorld" $string2 = "AlphaBravo" $string3 = "BreakMeUp" I need the following split at each upppercase letter for this output Hello World Alpha Bravo Break Me Up I've been trying to figure out how to split up strings such as the following: A54a1, 23b, FX49s1, R231z, 77 I would like to split them up and add them to individual arrays that are as follows: index[0] -> alphabetical characters, if any, prior to the first number, if any index[1] -> numerical characters, as a number, up until the following alphabetical character index[2] -> alphabetical character(s), if any index[3] -> numerical characters following the second alphabetical character set, if any So, for example, the above strings would be separated like this: A54a1 == ('A', 54, 'a', 1) 23b == (, 23, 'b',,) FX49s1 == ('FX', 49, 's', 1) R231z == ('R', 231, 'z',,) 77 == (,77,,) I've looked into things like strpos(), but I can't figure how to determine whether a character is alphabetic or numeric. And yes, I am familiar with the function is_numeric(). Just don't know how to throw it all together to accomplish what I need. hi everyone, i have a string as this one: 45,3455,66,8900,2,49,88,8,909 I need to print it with a new line every 4 numbers, as this one: 45,3455,66,8900, 2,49,88,8, 909 How can i do this? many thanks Hello, I have a string that contains a directory path much like: "/images/icons" and I want to take that string through a function and explode it into an array of directories that with add my directory structure array: images icons Then if I were to send the string "/data/documents" to the function it would append to the existing array: images icons data documents Here's the code that I've got thus far: <?php $global_structure = array(); generate_structure("./images/icons"); generate_structure("./data/documents"); function generate_structure($directory) { if(!is_dir($directory)) return false; $directory_structure = explode("/", $directory); // do something here to iterate over $global_structure and add new directories to equal to current directory path making the value an empty array. } ?> I am trying to figure out how to have an if statement in the middle of strings I am trying to concatenate. I have a from that has a address1 and address2, where address2 often does not exist. So I want to only add that line if there is actually content. I was trying something like the following, but that is giving me an error. I am pretty new to php, so I can't seem figure it out. Any help would be greatly appreciated. Code: [Select] "<p><strong>Address 1: </strong><br>" . $address1 . "<br>" if ( isset($address2) ) { echo . $address2 . "<br>" } . $city . ", " . $state . " " . $zip . "</p>" We have a database that shows for example: 2500-5000 5000-10000 10000-15000 15000-20000 I need to show in a screen what people are have 3000 followers, and as such as grouped into that 2500-5000. The best way I can see how to do this, is to run through the database table for these values. Then for each value, put the first and second set of numbers into two respective variables, say $low, $high. Then find those people who are on a particular platform with followers between those two values. So how do I turn $row->followsrange (which might be for example: "2500-5000"), into:
$low = 2500 Many thanks. ps I have a feeling the hyphen might play a bit part, as it cannot be done by the amount of characters.......... My string is like this: line 1 line 2 .... line 3453 line 3454 etc... I want to split the string into chunks of 500 lines, do something with those chunks and then reassamble the main $string again. The last chunk doesn't need to be 500 lines it can be less. How can I do it? Hi! So what I'm trying to do is split a multi dimensional array of strings ( each containing 3 strings inside of 1 array ) so I can write the results to a file. So say I had a bunch of strings inside 1 array : class File { function WriteFile ( $filename, $content ) { $list = [ $content ]; $delimiters = File :: checkDelimiter ( $content, true, false ); $fp = fopen ( $filename, 'w' ); foreach ( $list as $fields ) { $fields = File :: SplitFileByDelimiter ( $fields, ",", 3 ); file_put_contents ( $filename, $fields ); } fclose ( $fp ); } } $file-> File :: WriteFile ( $filepath . $filename, "TEST!, TEST!, TEST!, " . "TEST!, TEST!, TEST!, " . "TEST!, TEST!, TEST!" ); Instead of writing : TEST!, TEST!, TEST!, TEST!, TEST!, TEST!, TEST! ,TEST!, TEST!, TEST! to the file in a straight line, it should write it like this : TEST!, TEST!, TEST!, TEST!, TEST!, TEST!, TEST!, TEST!, TEST! Any help is ABSOLUTELY appreciated! Edited May 22 by AquariaXI Dear All PHP Master, Kindly need your help (I'm sorry my Bad English), I'm new in PHP and OOP in PHP please help to check I have class to convert number to words below, but when I'm running it the result in browser is error with error message : Notice: Undefined variable: str in C:\xampp\htdocs\latihan\oop.php on line 16 Notice: Undefined variable: str in C:\xampp\htdocs\latihan\oop.php on line 14 Notice: Undefined variable: str in C:\xampp\htdocs\latihan\oop.php on line 16 seratus dua puluh tiga juta empat ratus lima puluh enam ribu tujuh ratus delapan puluh sembilan here it's my code to convert number to words : <?php Class Terbilang { Public function terbilang() { $this->dasar = array(1=>'satu','dua','tiga','empat','lima','enam','tujuh','delapan','sembilan'); $this->angka = array(1000000000000,1000000000,1000000,1000,100,10,1); $this->satuan = array('triliun','milyar','juta','ribu','ratus','puluh',''); } Public function eja($n) { $i=0; while($n!=0){ $count = (int)($n/$this->angka[$i]); if($count>=10) $str .= $this->eja($count). " ".$this->satuan[$i]." "; else if($count > 0 && $count < 10) $str .= $this->dasar[$count] . " ".$this->satuan[$i]." "; $n -= $this->angka[$i] * $count; $i++; } $str = preg_replace("/satu puluh (\w+)/i","\\1 belas",$str); $str = preg_replace("/satu (ribu|ratus|puluh|belas)/i","se\\1",$str); return $str; } } $bilangan = new Terbilang; echo $bilangan -> eja(123456789); //shouldbe show :seratus dua puluh tiga juta empat ratus lima puluh enam ribu tujuh ratus delapan puluh sembilan ?> I have a regex that validates a string.
I want to ensure there is no space in the string.
Thus far I have used the negative look ahead regex construct as shown below to match strings without spaces.
(?!.*(\s))While this prevents space in between a word, all strings with space characters at the end or beginning are getting validated. I do not want that at all. This is the full regex script below: $string = "#JebiamgoOeing0"; if (preg_match('/^.*(?=.{8,})(?!.*([A-Za-z0-9])\1{1})(?=.*[a-z])(?=.*[A-Z])(?!.*(\s))(?=.*[\d])(?=.*[\W]).*$/', $string)) { "do something"; } else { "do something else"; }I need help in resolving this. Thank you. Edited by terungwa, 25 October 2014 - 12:50 PM. Hey I have a string that looks like the following: Quote top-php-tutorials-2.html I have a script that cycles through each page. The 2 in the quote above is the page number. How can I extract the number between the - and the .html and replace it with another number? I've tried Code: [Select] substr($engine->selectedcaturl, 0,-6).$v.".html"But then I realised this only works for numbers that are 1 digit long Any input would be appreciated I have the following code in my index.php: <?php $files = array(); $files = glob('download/*.iso'); $file = $files[count($files) - 1]; $strlen ( string $file ) : int /* length of $file */ $filelen = $strlen -9 /* length of unwanted characters - "download/" */ $filename = /* how do I get the filename starting at the 10th position */ ?> <div class="container"> <div class="divL"> <h3>Get the latest version of FoxClone iso</h3> <a href="<?php echo "/{$file}";?>"><img src="images/button_get-the-app.png" alt="" width="200" height="60"></a> I'd like to replace "Get the latest version of FoxClone iso" in the next to last line with "Get Foxclone "<?php echo "/{$filename}";?>". How do I extract just the file name from $file? I know that I have to:
1. get the length of $file Step 3 is where I just don't know how to accomplish the task. I'd appreciate some help on this.
Thanks in advance, Hey folks! Happy what ever you are celebrating I am trying to limit the number of characters to 200 that this array echos. I have tried a few things... I have tried using "substr($arr_tem, 0, 200)" but obviously it is not working... but I think I am close-ish. Any assistance would be great! Code: [Select] ###print array### echo (implode(", ", array_keys($arr_tem))); Thanks! Hey... I'm trying to echo the title of the news in the right column but I just want to display maximum of 25 characters how can I do it? thanks I'm pretty sure this is really simple to do using the count function, but I am having a hard time finding examples on how to count the number of times a "SPECIFIC" word occurs in a file. For example, I just want to display how many times "this-word" occurs in "http://www.mysite.com/logfile.log". Any help would be appreciated. |