I cant seem to figure the best approach to a PHP problem. I want to accomplish the following
I get a string that is, ie. 1000 characters in lenght
I want to split a string into 2.
The first string need to be 600 characters based on the following condition:
a) String should only be split if after a period
The second section of the string can be the remainder.
I know how to check the length of a string strlen($string) and I know how to explode a string into substrings using ie. explode(). However, I am not sure how to bring everything together.
I have used, its works.. you have try this...
<?php
$app_title="HIOX INDIA.COM, a leading business web hosting company, is
currently involved in web services, software/application development, web content
development, web hosting, domain registration, internet solutions and web design.";
echo "<br>Before :".$app_title;
$length=100;
if(strlen($app_title) > $length) {
$app_title1 = substr($app_title, 0,strpos($app_title, ' ', $length));}
$app_title2=split ( $app_title1 , $app_title);
echo "<br><br>After1 :".$app_title1;
echo "<br><br>After2 :".$app_title2[1];
?>
Use substr() to yank out everything after 600 chars from the original string.
Do a strpos() on that resulting sub-string to find the first .
Use the pos + 600 to do a substr on the original string and use that position as your split point.
Using substr() you can get part of a string defining a initial position and the lenght of the substring. For example:
Getting the first 600 characters:
$first = substr ( $input , 0 , 600 );
Getting the remaining:
$second = substr ( $input , 600 );
Related
I have a need to extract a sub-string from a longer string. I know how I would approach it using PHP posstr(); and strpos();, but the data is very large and I suspect that it would be more efficient if I could extract the part string using regex.
For example, if I have a number, (say a latitude) that has the format
"3203.79453"
where the the two characters before and "all" the characters after the decimal point represent decimal seconds, then to obtain the decimal latitude I need to compute the following:
32 + (03.79453)/60 = 32.06324217
So in essence I need a regex method of extracting the sub-string "03.79453".
So two questions how do I achieve it using regex and is it faster than using the method of using strpos() and posstr().
Thanks
It's easy to achieve with both options:
substr($line, strpos($line, '.') - 2);
or:
preg_match("/(\d{2}\..*)/", $line, $matches);
As for performance, I guess you would need to benchmark it. I've done a quick test to compare the performance of each example by running one million reps of each of those lines:
preg_match: average around 1.6 seconds for 1,000,000 matches
substr: average around 0.85 seconds for 1,000,000 matches
In this case it seems clear that using substr is the winner in terms of performance.
You could use preg_replace() like so:
<?php
$geoCoordinate = "3203.79453";
$degrees = preg_replace("#(\d{2}\.\d*?$)#", "", $geoCoordinate);
$seconds = preg_replace("#(\d*?)(\d{2}\.\d*?)#", "$2", $geoCoordinate);
$degAndSecs = round($degrees + ($seconds/60), 8);
var_dump($degAndSecs); //<== PRODUCES::: float 32.06324217
I am building a system to create dynamic descriptions for meta tags. It takes the post on the page and feeds it into a function which stripes out everything unnecessary and then takes the strlen see that its to large and creates a list of words. Now, I need to remove the right amount of words to bring the string down to 155 characters or 152 and I will add an ellipsis.
Example String (None of this is actual code its meant for sudo code)
$string = "Hello lovely Solia Avatar Community, I have a little problem and I need your help. I used to have Paint Tool SAI but my laptop ate a lot of my files, one of them being SAI. Now I am trying to get it back but I lost the website I got it from. I keep finding a website to buy it from for about $70.";
echo strlen($string); = 296
if(strlen($string) > 155) {
// Get word amount
$words = preg_split('/\s+/', ltrim($string), 155 + 1);
}
Now, I have the words in an array and I need to take that array and bring it down to a total strlen of 155 and stop at the nearest word and not break it awkwardly. Maybe I am going about trying to solve this problem incorrectly and I need to be using a different set of functions.
The basic idea is to find the position of the first space after the first 155 characters. This can be done with strpos($string, ' ', 155). Then use substr($string, 0, $endat155) to retrieve the portion of the string from the beginning to that position.
$endat155 = strpos($string, ' ', 155);
$firstWords = substr($string, 0, $endat155);
echo $firstWords;
I have some expressions of the form aa/bbbb/c/dd/ee. I want to select only the part dd from it using a php code. Using 'substr' it can be done, but the problem is that the lengths of bbbb can vary from 3 (i.e., bbb) to 4, lengths of c can be 1 or 2 and the lengths of dd can be 2, 3 or 4. Then how can I extract the part dd (i.e, the part between the last pair / /).
Use explode to explode the string into an array and then grab the 4th item in the array which will be dd regardless of the size of the other elements, just make sure the number of '/' stays the same
If the structure of the expression always has the "/" separators even if the values in between are varying in length (or sometimes absent) you can use explode().
$parts_array = explode("/", $expression);
$dd = parts_array[3];
If the number of slashes varies, you'll have to do more work, like determining how many slashes there are and what parts of the expression are missing. That's a fair bit more complex.
Here you go, the PHP script for selecting the text between the last pair of "/../":
<?php
$expression = "aaa/vvv/bbbb/cccc/ddd/ee";
$mystuff = explode("/", $expression);
echo $mystuff[sizeof($mystuff)-2];
?>
I hope it helps. Good luck!
Here is the string:
Added: yesterday Closing: 17.02.2013
there may be variations of the above string such as :
Added: today Closing: 04.02.2013
Added: 16.01.2013 Closing: 15.02.2013
I need to be able to get the "yesterday" and "17.02.2013" or "today" and "04.02.2013" or "16.01.2013" and "15.02.2013"
right now here is what i got..
$pieces = explode(" ", $initialDate);
echo $pieces[1]; // piece2
echo $pieces[3]; // piece2
echo '<br/><br/>';
but i get only "today" and "closing" , should have got the closing date, but its not working..
should i have done it using regex?
explode can be used, in this case. The keys and values come in pairs with the key preceding the value, so look for the keyword and then take the next string as the value. I'd probably return it as an array of the form (key1 =>value1, key2=>value2,)
When it works explode is likely to be faster than regex, but the speed isn't a big factor in most cases.
Edit: the example has multiple spaces between the elements, these need to be removed first or empty items skipped in the result.
There are too much spaces in your string to make it in the order you expect.
To keep with explode(), do not blindly address an array member with guessed number but print_r($pieces); first, then notice the right number, then use it
If the number of spaces can vary - regexp is the only [sensible] choice. Though it can be combined with regexp by using simple $pieces = preg_split('#\s#',$initialDate);
You can do it using regular expression
$string="Added: today Closing: 04.02.2013";
$result = preg_match_all("/([a-z A-z])*day|((\d{2}\.){2}(\d{4}))/", $string,$match);
print_r($match[0]);
output
Array ( [0] => today [1] => 04.02.2013 )
pattern "/([a-z A-z])*day/" match string ends with day
pattern "((\d{2}.){2}(\d{4}))" matches date of the form (dd.mm.yyyy) or (mm.dd.yyyy)
Live Demo
Exploded is just fine, as long as the input does not change. Regex are fine too, but they usually make the code hard to read and slow down the application. It seems you are missing the double-spaces in your text, that creates a empty string in your result of explode. Checkout the contents with var_dump, print_r or better use debugging.
foreach(explode("\n", $str) as $line)
print_r(explode(' ', $line));
Im reluctant to ask but I cant figure out php preg_replace and ignore certain bits of the sting.
$string = '2012042410000102';
$string needs to look like _0424_102
The showing numbers are variable always changing and 2012 changes ever year
what I've tried:
^\d{4}[^\d{4}]10000[^\d{3}]$
^\d{4}[^\d]{4}10000[^\d]{3}$
Any help would be appreciated. I know it's a noob question but easy points for whoever helps.
Thanks
Your first regex is looking for:
The start of the string
Four digits (the year)
Any single character that is not a digit nor { or }
The number 10000
Any single character that is not a digit nor { or }
The end of the string
Your second regex is looking for:
The start of the string
Four digits (the year)
Any four characters that are not digits
The number 10000
Any three characters that are not digits
The end of the string
The regex you're looking for is:
^\d{4}(\d{4})10000(\d{3})$
And the replacement should be:
_$1_$2
This regex looks for:
The start of the string
Four digits (the year)
Capture four digits (the month and day)
The number 10000
Capture three digits (the 102 at the end in your example)
The end of the string
Try the following:
^\d{4}|10000(?=\d{3}$)
This will match either the first four digits in a string, or the string '10000' if there are three digits after '10000' before the end of the string.
You would use it like this:
preg_replace('/^\d{4}|10000(?=\d{3}$)/', '_', $string);
http://codepad.org/itTgEGo4
Just use simple string functions:
$string = '2012042410000102';
$new = '_'.str_replace('10000', '_', substr($string, 4));
http://codepad.org/elRSlCIP
If they're always in the same character locations, regular expressions seem unnecessary. You could use substrings to get the parts you want, like
sprintf('_%s_%s', substr($string,4,4), substr($string,13))
or
'_' . substr($string,4,4) . '_' . substr($string,13)