4

This is my string

#Jhon: Manager #Mac: Project Manager #Az: Owner

And I want array something like this

$array = ['0' => 'Manager', '1' => 'Project Manager', '2' => 'Owner']

I tried this but each time return only 'Manager'

$string = '#Jhon: Manager #Mac: Project Manager #Az: Owner';
getText($string, ':', ' #')
public function getText($string, $start, $end)
{
  $pattern = sprintf(
      '/%s(.+?)%s/ims',
      preg_quote($start, '/'), preg_quote($end, '/')
  );

  if (preg_match($pattern, $string, $matches)) {
      list(, $match) = $matches;
      echo $match;
  }
} 
3

4 Answers 4

8

You may preg_split the contents and use the following solution:

$re = '/\s*#[^:]+:\s*/';
$str = '#Jhon: Manager #Mac: Project Manager #Az: Owner';
$res = preg_split($re, $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($res);

See the PHP demo and a regex demo.

Pattern details:

  • \s* - 0+ whitespaces
  • # - a literal # symbol
  • [^:]+ to match 1+ chars other than :
  • : - a colon
  • \s* - 0+ whitespaces.

Note that -1 in the preg_split function is the $limit argument telling PHP to split any amount of times (as necessary) and PREG_SPLIT_NO_EMPTY will discard all empty matches (this one may be removed if you need to keep empty matches, that depends on what you need to do further).

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks wiktor.. but when my string is '#Jhon: Manager #[email protected]: Project Manager #Az: Owner' not working.. sorry for not specify this possibility
@Komal: But I already wrote - use [^:\s]+ in that case, use \s*#[^:\s]+:\s*. I removed the \w-based regex variation.
One more possibility is space between name '#Jhon M: Manager #[email protected]: Project Manager #Az: Owner'
@Komal: Then how can you differentiate # as the starting point of the key and a # that is part of the value?
Ok, remove \s from the [^:\s]+. See this demo.
|
6

Here we are using preg_match to achieve desired output.

Regex: #\w+\s*\:\s*\K[\w\s]+

1. #\w+\s*\:\s*\K this will match # then words then spaces and then : \K will reset current match.

2. [\w\s]+ This will match your desired output which contains words and spaces.

Solution 1:

<?php
ini_set('display_errors', 1);
$string="#Jhon: Manager #Mac: Project Manager #Az: Owner";
preg_match_all("/#\w+\s*\:\s*\K[\w\s]+/", $string,$matches);
print_r($matches);

Output:

Array
(
    [0] => Array
        (
            [0] => Manager 
            [1] => Project Manager 
            [2] => Owner
        )

)


Here we are using array_map and explode to achieve desired out. Here we first we are exploding string on # and then exploding its array values on : and pushing its first index in the resultant array.

Solution 2:

Try this code snippet here

<?php

$string="#Jhon: Manager #Mac: Project Manager #Az: Owner";
$result=  array_map(function($value){
    return trim(explode(":",$value)[1]);
}, array_filter(explode("#", $string)));
print_r(array_filter($result));

Output:

Array
(
    [1] => Manager
    [2] => Project Manager
    [3] => Owner
)

4 Comments

@SahilGulati 2nd Gives me error Undefined offset: 1, and first is working but not worked when my string is '#Jhon: Manager #[email protected]: Project Manager #Az: Owner' sorry for not specifing this possibility..
@Komal Hope this will help you out.. I have updated my post for that for demo you can check this eval.in/787827
This way is indeed better since the pattern starts with a literal character. You can remove the eventual trailing spaces using: regex101.com/r/tgjssx/1
@Casimir just for my safe side i added this so that OP will not face any further issues with other string input which may contain spaces.. Anyways thanks sir...:)
0

You can use explode() function to split the string by char. After that just replace the elements of new array with preg_replace().

Comments

0

Try this

$string = '#Jhon: Manager #Mac: Project Manager #Az: Owner';
$res = explode("#", $string);
$result = array();
for($i = 1; $i < count($res); $i++)  {
    $result[] = preg_replace("/(^[A-Z][a-z]*: )/", "", $res[$i]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.