2013-08-14 3 views
0

트위터와 인스 타 그램과 같은 시스템을 사용하여 @johndoe를 넣는 곳 "@"와이 문자 사이의 이름까지 줄이려고 할 수 있습니다. ?, ,, ], :, 예를 heres 내 문자열로 (space)PHP에서 2 문자 사이에 부분 문자열 가져 오기

다음에 문자없이 내가 janeDoe의 배열을 얻을 수있는 방법 hey @johnDoe check out this event, be sure to bring @janeDoe:,@johnnyappleSeed?, @johnCitizen] , and @fredNerk

, johnnyappleSeed, johnCitizen, fredNerk ers ?, ,, ], :이 첨부되어 있습니다.

나는 preg_match의 변형을 사용해야한다는 것을 알고 있지만 그 사실을 강력하게 이해하지 못합니다.

+1

https://xkcd.com/208/ –

+0

의 일부 구성 요소 "@"로 시작하고 ","로 끝나지 말고 "{space } ". – DevlshOne

답변

1

이것은 당신이 요구 한 무엇을 : /\@(.*?)\s/
이 당신이 정말로 원하는 것입니다 : /\b\@(.*?)\b/

넣어 두 preg_match_all()에 하나 결과 배열을 평가합니다.

+0

나는 질문을 업데이트했다. 나는 내 질문에 다음 (공백)까지 문자열을 물어 보았다. 실제로 그 마지막 문자를 바꾸고 싶다. ("", "?", "[", ":" ",") 당신의 대답은 내가 필요한 것에 가장 가깝습니다. –

1
preg_match_all("/\@(.*?)\s/", $string, $result_array); 
0
$check_hash = preg_match_all ("/@[a-zA-Z0-9]*/g", $string_to_match_against, $matches); 

그런 다음

foreach ($matches as $images){ 
    echo $images."<br />"; 
} 

UPDATE처럼 뭔가를해야만 할 수있는 : 당신이 잘못된 문자를 제거하기 위해 찾고 그냥 깨달았다. 업데이트 된 스크립트가이를 수행해야합니다.

0

방법에 대해 :

$str = 'hey @johnDoe check out this event, be sure to bring @janeDoe:,@johnnyappleSeed?, @johnCitizen] , and @fredNerk'; 
preg_match_all('/@(.*?)(?:[?, \]: ]|$)/', $str, $m); 
print_r($m); 

출력 :

Array 
(
    [0] => Array 
     (
      [0] => @johnDoe 
      [1] => @janeDoe: 
      [2] => @johnnyappleSeed? 
      [3] => @johnCitizen] 
      [4] => @fredNerk 
     ) 

    [1] => Array 
     (
      [0] => johnDoe 
      [1] => janeDoe 
      [2] => johnnyappleSeed 
      [3] => johnCitizen 
      [4] => fredNerk 
     ) 

) 

설명 :

The regular expression: 

(?-imsx:@(.*?)(?:[?, \]: ]|$)) 

matches as follows: 

NODE      EXPLANATION 
---------------------------------------------------------------------- 
(?-imsx:     group, but do not capture (case-sensitive) 
         (with^and $ matching normally) (with . not 
         matching \n) (matching whitespace and # 
         normally): 
---------------------------------------------------------------------- 
    @      '@' 
---------------------------------------------------------------------- 
    (      group and capture to \1: 
---------------------------------------------------------------------- 
    .*?      any character except \n (0 or more times 
          (matching the least amount possible)) 
---------------------------------------------------------------------- 
)      end of \1 
---------------------------------------------------------------------- 
    (?:      group, but do not capture: 
---------------------------------------------------------------------- 
    [?, \]: ]    any character of: '?', ',', ' ', '\]', 
          ':', ' ' 
---------------------------------------------------------------------- 
    |      OR 
---------------------------------------------------------------------- 
    $      before an optional \n, and the end of 
          the string 
---------------------------------------------------------------------- 
)      end of grouping 
---------------------------------------------------------------------- 
)      end of grouping 
---------------------------------------------------------------------- 
관련 문제