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 (?=...)
Related
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:
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 have this array
array (size=7)
0 => string 'Â bs-0468R(20UG)' (length=16)
1 => string 'Â bs-1338R(1ML)' (length=15)
2 => string 'Â bs-1557G(NO BSA)' (length=18)
3 => string 'Â bs-3295R(NO BSA)' (length=18)
4 => string '" bs-0730R' (length=10)
5 => string '" bs-3889R' (length=10)
6 => string 'bs-0919R (NO BSA)' (length=17)
I want to throw away everything and only keep the string that start with bs.
What is the best of doing it ?
Something like this:
$result = array_filter($array, function ($i) {
return strpos($i, 'bs')===0;
});
I love preg_grep:
$result = preg_grep('/^bs/', $array);
I agree with #Casimir et Hippolyte. If you know you're always going to have a controlled dataset such as your example (rare), you can always just reference the string as an array -- which it already is under the hood:
$result = array_filter($array, function ($v) {
return $v[0] . $v[1] == 'bs';
});
Regex is amazing and not a performance problem for most situations, however I have had some issues with it where other functionalities were far faster and efficient when it counted. I understand that statement is not true for the majority of applications, but it is worth noting.
I'm using a sphinx bundle (timewasted SphinxSearchBundle) in a symfony 2 app (https://github.com/timewasted/Search-SphinxsearchBundle) (based on the PHP API)
it works great as long as I don't try to use the SPH_MATCH_EXTENDED.
Here's a code sample
$sphinxSearch = $this->get('search.sphinxsearch.search');
$sphinxSearch->setMatchMode(SPH_MATCH_EXTENDED);
$searchResults = $sphinxSearch->search("#typemesure_id 2", $index);
But the result is always empty, and it seems that my field (#typemesure_id) is considered as a word :
'words' =>
array
'typemesure_id' =>
array
'docs' => string '0' (length=1)
'hits' => string '0' (length=1)
2 =>
array
'docs' => string '4169' (length=4)
'hits' => string '5714' (length=4)
Does anyone konws whats wrong in my request ?
Can anyone post an exemple of working extended query working with this bundle?
My goal is to get a condition like "#(user1,user2,user3) 15"
Thanks for your help
Notes:
the SPH_MATCH_ALL & SPH_MATCH_ANY are working fine.
The setFilter() method is also working
Looking at the function defition...
public function search($query, array $indexes, array $options = array(), $escapeQuery = true)
It will automaticlly escape the query, so the # will be getting escaped. Need to pass false as fourth param
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!