Hello
I have one text like:
"shz01unbmf logged out, 4357 16945723 35427886 42338 49265"
now I need "shz01unbmf"
How can I get it? (Best way)
8 replies to this topic
#1
Posted 23 August 2011 - 06:31 AM
|
|
|
#2
Posted 23 August 2011 - 06:40 AM
At the moment I can think of two ways of achieving this:
Haven't done any benchmarks but I imagine the first one should be more efficient since it doesn't have to read the whole string.
$string = "shz01unbmf logged out, 4357 16945723 35427886 42338 49265"; $whatYouWant = substr($string, 0, strpos($string, ' ')); echo $whatYouWant; //shz01unbmf
$parts = explode(' ', "shz01unbmf logged out, 4357 16945723 35427886 42338 49265")
echo $parts[0] //shz01unbmf
Haven't done any benchmarks but I imagine the first one should be more efficient since it doesn't have to read the whole string.
#3
Posted 23 August 2011 - 06:42 AM
If "shz01unbmf" cannot contain space you have many way to do so.
You can explode your string
Or use a substr with a strpos
But if shz01unbmf can contain space, you should use the word logged to set your delimiter
Once again you could use an explode using the word "logged" insted of space
Or substr with a strpos like so
I think the best way is to use the word logged with a strpos & substr
You can explode your string
$array = explode(' ', $string); echo ($array[0]);
Use some regular expression (I'm not too good with this, so I can't give you an example)Or use a substr with a strpos
But if shz01unbmf can contain space, you should use the word logged to set your delimiter
Once again you could use an explode using the word "logged" insted of space
Or substr with a strpos like so
$pos = strpos($str, 'logged'); if ($pos !== 0) $output = substr($str, 0, $pos);
I think the best way is to use the word logged with a strpos & substr
#4
Posted 23 August 2011 - 08:59 AM
I will test both of them because I have more than 3000 record to do this job.
#5
Posted 23 August 2011 - 12:18 PM
#6
Posted 24 August 2011 - 02:30 AM
John said:
Substr will use less memory.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#7
Posted 24 August 2011 - 02:05 PM
My thinking, and please do tell me where I am wrong (my knowledge of PHP internals is limited):
In the case above, explode() will return 8 strings each individually packed into a zval whereas substr()/strpos() will only return 1 (packed into a zval). Although we are only talking about 8 bytes for the each zval union, the size of the internal hash table, and a pointer to each zval[?] (agreeably, in this situation it is negligible) - it really adds up quickly. Try doing an explode() on War and Peace, and I'm sure you will be surprised it uses 50mb or RAM.
As we can see, PHP uses an initial 326216B to start up. Then we read in the file (3627268 - 326216)B. Then we observe memory usage after reading the file and doing the substr()/strpos() is nothing/negligible because substr()/strpos() only requires an few additional bytes (8 bytes for the zval plus 3 bytes for the word "The") for the returned string. However, explode() increases the memory usage by a factor of 10.
In the case above, explode() will return 8 strings each individually packed into a zval whereas substr()/strpos() will only return 1 (packed into a zval). Although we are only talking about 8 bytes for the each zval union, the size of the internal hash table, and a pointer to each zval[?] (agreeably, in this situation it is negligible) - it really adds up quickly. Try doing an explode() on War and Peace, and I'm sure you will be surprised it uses 50mb or RAM.
<?php
echo memory_get_peak_usage(), "\n";
$file = file_get_contents("http://www.gutenberg.org/files/2600/2600.txt");
echo memory_get_peak_usage(), "\n";
$str = substr($file, 0, strpos($file, ' '));
echo memory_get_peak_usage(), "\n";
$array = explode(" ", $file);
echo memory_get_peak_usage(), "\n";
?>Quote
326216
3627268
3627268
51745248
3627268
3627268
51745248
As we can see, PHP uses an initial 326216B to start up. Then we read in the file (3627268 - 326216)B. Then we observe memory usage after reading the file and doing the substr()/strpos() is nothing/negligible because substr()/strpos() only requires an few additional bytes (8 bytes for the zval plus 3 bytes for the word "The") for the returned string. However, explode() increases the memory usage by a factor of 10.
#8
Posted 25 August 2011 - 12:38 AM
My intention was to infer a theoretical iterative solution and its peak memory per iteration, if the file or set of data were to be parsed and names were to be collected for whichever purpose.
Your method does indeed show that explode will not stop, as does the other solution on the long sample, however this would be negated by the fact that only a small portion (plus the entire data set) will be stored in memory at one time.
Utilising both the first element of the return of explode versus the two calls to substr and strpos, the end result, minus any interference from reading the file itself in to memory is the following peak (in kilobytes)
explode = 5662, substr = 5649
This difference is especially negligible in his situation.
Your method does indeed show that explode will not stop, as does the other solution on the long sample, however this would be negated by the fact that only a small portion (plus the entire data set) will be stored in memory at one time.
Utilising both the first element of the return of explode versus the two calls to substr and strpos, the end result, minus any interference from reading the file itself in to memory is the following peak (in kilobytes)
explode = 5662, substr = 5649
This difference is especially negligible in his situation.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#9
Posted 25 August 2011 - 05:27 AM
Touché. I should note that the memory in my explode example can be reduced by using the third $limit argument by using a value of two. However, the entire string still has to be duplicated for the return value, but this time with the overhead of only two zval's.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









