Jump to content

Text before space?

- - - - -

  • Please log in to reply
8 replies to this topic

#1
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
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)

#2
Revolt

Revolt

    Programmer

  • Members
  • PipPipPip
  • 99 posts
At the moment I can think of two ways of achieving this:

$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
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
If "shz01unbmf" cannot contain space you have many way to do so.
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
Hamed

Hamed

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 276 posts
I will test both of them because I have more than 3000 record to do this job.

#5
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
Substr will use less memory.

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200

John said:

Substr will use less memory.
explode() will require no less than the equivalent memory, and only slightly more on his application - both require the string to remain in memory in contrast to what is actually being used. The overhead of explode will be negligible compared to the parent operations prior to that.
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.

#7
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
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.


<?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

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
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
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.
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.

#9
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
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