Reputation: 1751
I have a string that i need to cut the string after the first '' or space. Example: Demo Road Anytown EC4 5RT
. I need to just have Demo Road and cut the rest.
I have looked at chop, explode, substr but cannot seem to get it to work. I would be grateful if someone could help with this request. Many thanks
Upvotes: 0
Views: 90
Reputation: 4028
Obviously, you want to split a malformed address into its parts.
I call it malformed, because it does not contain separators. If the string was Demo Road, Anytown, EC4 5RT
, the job was easy as
$address = 'Demo Road, Anytown, EC4 5RT';
$parts = preg_split('~,\s*~', $address);
$street = $parts[0];
$town = $parts[1];
$zip = $parts[2];
Of course the code needs some sanity checks, but you get the point. So, if you have influence on the input format, use separators.
If you can't use separators, you need a dictionary of all valid town names and ideally all street names in each town.
Imagine this string:
24 Upper Demo Road South Anytown EC4 5RT
It could be any of
24 Upper, Demo Road South Anytown, EC4 5RT
24 Upper Demo, Road South Anytown, EC4 5RT
24 Upper Demo Road, South Anytown, EC4 5RT
24 Upper Demo Road South, Anytown, EC4 5RT
In essence, the only thing you know is that the first non-numeric word belongs to the street name, and the last word not being the zip belongs to the town.
Now, for any possible town name, check the dictionary, if the town exists and has a corresponding street. As soon as you find a valid combination, you're done. If no suitable combination exists, your business logic has to decide the consequences.
Everything else is just guesswork and will fail more often that you want.
Upvotes: 0
Reputation: 408
Do this:
<?php
//The number of words needed. Here, 2.
$wordNeeded = 2;
$a = 'Demo Road Anytown EC4 5RT';
$b = explode(" ",$a);
//The reconstructed string will be inside.
$reconstructedString = '';
//Loops over $b and check if $wordNeeded is not too high
//and reconstructs the string.
for ($i = 0; $i < $wordNeeded && isset($b[$i]); $i++) {
$reconstructedString .= $b[$i];
//Adds the space, but not after the last word.
if ($i < $wordNeeded -1) {
$reconstructedString .= ' ';
}
}
echo $reconstructedString; // Demo Road
The code explodes your string into an array of words using explode and then loop over the array to reconstruct the string. If you need the third word too, the only thing to change is $wordNeeded
.
Upvotes: 1
Reputation: 19764
To get the two first words, you could use :
$num = 2; // get two first words
$str = "Demo Road Anytown EC4 5RT";
echo implode(' ', array_slice(explode(' ', $str, $num + 1), 0 , $num)) ; // "Demo Road"
That code explodes your string on the space so the returned array has the two first words and the rest of the string. Then using array_slice only the two first words are kept in the array, the rest of the string is no longer here. Finally, the string is reconstructed using implode.
Or you could use str_word_count()
:
$num = 2;
$str = "Demo Road Anytown EC4 5RT";
echo implode(' ', array_slice(str_word_count($str, 1), 0, $num)) ; // "Demo Road"
Here it's almost the same but str_word_count is used instead of exploding manually the string into words.
Thanks @AnthonyB for the great edit !
Upvotes: 2