Viewed   97 times

Any ideas how to split string into 3 parts where by two "delimiters" where the first and the last "delimiters" are letters or numbers respectively?

$str = "%@-H-e-l-l-o-7-9#$%";

would be split like this:

$arr=("%@-","H-e-l-l-o-7-9", "#$%");

and

$str = "Hi$73";

would be split like this:

$arr=("","Hi$73", "");

and

$str = "????????!";

would be split like this:

$arr=("","????????", "!");

and

$str = "!";

would be split like this:

$arr=("!","", "");

and

$str = "";

would be split like this:

$arr=("","", "");

and

$str = "?55?W";

would be split like this:

$arr=("","?55?W", "");

which means it returns an array that consists of 3 elements (always), and the first and last symbols of the second element are numbers or latin/cyrillic letters, and the first and last(third) elements of this array contain absolutely no numbers and letters, and the join of that strings is the source string

Thank you.

 Answers

5

Here is a way to do the job:

$in = array(
"%@-H-e-l-l-o-7-9#$%",
"Hi$73",
"????????!",
"!",
"",
"?55?W",
'$abc$$$',
"?????_",
"34.5",
'#_!',
);

foreach($in as $elem) {
    preg_match('/^([^pLpN]*)((?=[pLpN]|$)[^_]*(?<=[pLpN])|^)?([^pLpN]*)$/u', $elem, $m);
    printf("'%15s'%s'%10s't%s'%10s't%s'%10s'%s", "$elem","=> (1): ",$m[1],"(2): ",$m[2], "(3): ",$m[3],"n");

}

Where:

  • pL stands for any letter in any language
  • pN stands for any number in any language

Output:

'%@-H-e-l-l-o-7-9#$%'=> (1): '       %@-'   (2): 'H-e-l-l-o-7-9'    (3): '       #$%'
'          Hi$73'=> (1): '          '   (2): '     Hi$73'   (3): '          '
'????????!'=> (1): '          ' (2): '????????' (3): '         !'
'              !'=> (1): '         !'   (2): '          '   (3): '          '
'               '=> (1): '          '   (2): '          '   (3): '          '
'        ?55?W'=> (1): '          ' (2): '   ?55?W' (3): '          '
'        $abc$$$'=> (1): '         $'   (2): '       abc'   (3): '       $$$'
'    ?????_'=> (1): '          '    (2): '?????'    (3): '         _'
'           34.5'=> (1): '          '   (2): '      34.5'   (3): '          '
'            #_!'=> (1): '       #_!'   (2): '          '   (3): '          '
Friday, September 16, 2022
 
1

How about:

$sentence = "When it comes time to renew your auto insurance policy, be aware of how your carrier handles renewals";
$searches = array('aware', 'aware of', 'be aware', 'be aware of');
$replaces = array('conscious', 'conscious of', 'remember', 'concentrate on');

function cmp($a, $b) {
    if (strpos($a, $b) !== false) return -1;
    if (strpos($b, $a) !== false) return 1;
    return 0;
}

uasort($searches, 'cmp');
$replaces_new = array();
$i=0;
foreach($searches as $k=>$v) {
    $replaces_new[$i] = $replaces[$k];
    $i++;
}

$res = str_replace($searches, $replaces_new, $sentence);
echo $res;

output:

When it comes time to renew your auto insurance policy, concentrate on how your carrier handles renewals
Saturday, August 20, 2022
 
1

When you use preg_match_all, $matches is a 2-dimensional array. So $matches[1] and $matches[3] are both arrays. echo only works with numbers or strings, so you get a warning when you try to echo an array. If you want to see what's in them, use print_r() or var_dump():

print_r($description);
print_r($language);
Thursday, September 8, 2022
3

Here you go

var yourString = "/installers/";
var result = yourString.substring(1, yourString.length-1);

console.log(result);

Or you can use .slice as suggested by Ankit Gupta

var yourString = "/installers/services/";

var result = yourString.slice(1,-1);

console.log(result);

Documentation for the slice and substring.

Monday, September 12, 2022
3

If you're talking about line-breaking, take a look at Dynamic Line Breaking, which gives a Dynamic Programming solution to divide words into lines.

Wednesday, October 5, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :