I have this text 'Amber Costa Rica' and I want to separate it in 'Amber' and 'Costa Rica'
I'm using with php
preg_match('/(.*) (.*)/', $details_as_text, $matches);
but it gives me
array (size=3)
0 => string 'Amber Costa Rica' (length=16)
1 => string 'Amber Costa' (length=11)
2 => string 'Rica' (length=4)
I'm sure this is simple, but i've been searching without success.
Instead of .* you can use [^ ]*? (or \S*?) which specifically selects everything until there's space.
(\S*?)\s+(.*)
Here's a live preview
Which correctly matches:
Related
I'm currently working as a php dev, and now has an assignment with some old php legacy code that's intended to filter certain car details before adding it into the DB.
What I'm currently stuck on is how I'm supposed to skip splitting the models inside of the parenthesis
Example:
"v70, 790, v50 (v40, v44), v22"
Expected output:
[ "v70", "790", "v50 (v40, v44)", "v22" ]
So that the , inside of the parentheses is disregarded by the split.
Any help and pointers is greatly appreciated!
You can use preg_split() method for this (documentation). You can use this to split the string based on a regex pattern for comma separated values but ignored if these are between parentheses.
This code works for your example:
<?php
$string = 'v70, 790, v50 (v40, v44), v22';
$pattern = '/,(?![^(]*\)) /';
$splitString = preg_split($pattern, $string);
Output of $splitString looks like:
array (size=4)
0 => string 'v70' (length=3)
1 => string '790' (length=3)
2 => string 'v50 (v40, v44)' (length=14)
3 => string 'v22' (length=3)
I need some help. What I want is to make ignore a comma in specific string. It is a comma seperated file csv, but the name have a comma, and I need to ignore that.
What I got is
<?php
$pattern = '/([\\W,\\s]+Inc.])|[,]/';
$subject = 'hypertext language, programming, Amazon, Inc., 100';
$limit = -1;
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;
$result = preg_split ($pattern, $subject, $limit, $flags);
?>
Result is
$result (php code):
<?php
array (
0 => 'hypertext language',
1 => ' programming',
2 => ' Amazon',
3 => ' Inc.',
4 => ' 100',
);
?>
And I want the result to be
$result (php code):
<?php
array (
0 => 'hypertext language',
1 => ' programming',
2 => ' Amazon, Inc.',
3 => ' 100',
);
?>
Thanks for your help :)
Note that [\W,\s] = \W since \W matches any char that is not a letter, digit or underscore. However, it seems you just want to split on a , that is not followed with space(s)*+Inc..
You may use a negative lookahead to achieve this:
/,(?!\s*Inc\.)/
^^^^^^^^^^^^
See the regex demo
The (?!\s*Inc\.) will fail any , match if there are 0+ whitespaces (\s*) followed with a sequence of literal characters Inc. after them.
From your tutorial, if I pull the Amazon information as a CSV, I get the following format. Which you can then parse with one of Php's native functions. This shows you don't need to use explode or regex to handle this data. Use the right tool for the job:
<?php
$csv =<<<CSV
"amzn","Amazon.com, Inc.",765.56,"11/2/2016","4:00pm","-19.85 - -2.53%",10985
CSV;
$array = str_getcsv($csv);
var_dump($array);
Output:
array (size=7)
0 => string 'amzn' (length=4)
1 => string 'Amazon.com, Inc.' (length=16)
2 => string '765.56' (length=6)
3 => string '11/2/2016' (length=9)
4 => string '4:00pm' (length=6)
5 => string '-19.85 - -2.53%' (length=15)
6 => string '10985' (length=5)
I am trying to extract URL out of a string. The format of the string will be:
some text! some numbers http://linktoimage.com/image
I found this post earlier Extract URLs from text in PHP
and I think this solution mentioned there could work:
<?php
$string = "this is my friend's website http://example.com I think it is coll";
echo explode(' ',strstr($string,'http://'))[0]; //"prints" http://example.com
However I do not understand what it actually does. Would someone mind explaining this to me to me ?
You have this string:
this is my friend's website http://example.com I think it is coll
strstr($string,'http://') will return
http://example.com I think it is coll
explode(' ', ...) then will split this resulting string at the space character resulting in
array(
0 => 'http://example.com',
1 => 'I',
2 => 'think',
3 => 'it',
4 => 'is',
5 => 'coll'
)
and finally [0] returns the first item of this array, which is:
http://example.com
Further reading:
http://php.net/manual/en/function.strstr.php
http://php.net/manual/en/function.explode.php
I currently have this URL pattern:
foo-bar-1
bar-foo-bar-2
etc
I'd like to get the words separated from the number but I'm having some trouble completely understanding how to do that.
I started with this which got me close:
$slug = 'foo-bar-1';
preg_split('/(\d+)$/', $slug);
array (size=2)
0 => string 'foo-bar-' (length=8)
1 => string '' (length=0)
But I can't seem to finish it up. I'd like it to be this:
array (size=2)
0 => string 'foo-bar' (length=7)
1 => string '1' (length=1)
Any help is greatly appreciated! Thank you!
Try this:
preg_split('/-(?=\d+$)/', $slug);
I use - as separator and I check if it is followed by a number at the end of the string with a lookahead (?=...)
I have a string with many caracters and I need obtain data from it.
First of all, I did explode by ';', now I have an array and of each row I have a word into quotes.
I want to remove all, less this word into quotes. I know that is more easy to obtain these words with preg_match, but how is into an array, to save up to go over the array again, I would like to clean it directly with preg_replace.
$array = explode(';', $string);
//36 => string 's:7:"trans_1"' (length=13)
//37 => string 's:3:"104"' (length=9)
//38 => string 's:5:"addup"' (length=11)
//39 => string 's:1:"0"' (length=7)
$array = preg_replace('! !i', '', $array);
I would like to obtain:
//36 => string 'trans_1' (length=6)
//37 => string '104' (length=3)
//38 => string 'addup' (length=5)
//39 => string '0' (length=1)
I tryed differents things, but I can't rid off the letters outside the quotes.
While this isn't a direct answer to your question it solves your problem. The data you are looking at came from the php function serialize() to retrieve the data from that string you need to use the php function unserialize().
$data = unserialize($string);
You could try
preg_replace('!.*"([^"]*)".*!i', '\1', $array);
\1 refers to the first captured group!