2014-04-14 3 views
0

특정 정보에 대한 전자 메일 제목을 구문 분석하고 있습니다. 아래 함수는 2 개의 문자열 사이에있는 문자열을 반환합니다. 6588 이 나는 ​​물론, 원하는 것을 정확히 :와 결과2 문자열 사이에서 문자열 찾기

<?php 

function find_string_between_characters($haystack, $starter, $ender) { 
    if(empty($haystack)) return ''; 
    $il = strpos($haystack,$starter,0)+strlen($starter); 
    $ir = strpos($haystack,$ender,$il); 
    return substr($haystack,$il,($ir-$il)); 
} 

$string = 'Re: [Ticket #6588] Site security update'; 
$begin = '[Ticket #'; 
$end = ']'; 
echo find_string_between_characters($string, $begin, $end); // result: 6588 
?> 

. 나는 겨우 발견이 나는이 같은 변수를 변경하는 경우 :

<?php 
$string = 'New Ticket Email Submitted'; 
$begin = '[Ticket #'; 
$end = ']'; 
echo find_string_between_characters($string, $begin, $end); // result: t Email 
?> 

과 결과 : t 이메일

나는 모두 내부 문자의 정확한 순서를보고 기능을 조정하려면 어떻게 $$ end 변수를 시작 하시겠습니까?

+1

같은 간단한는 preg_match를 preg_match를 사용할 수있다() 쉬울 –

+0

이 두둑가 일정, 즉 [티켓 # 6588]? –

+0

네, 운 좋게 꽤 상수입니다. – coffeemonitor

답변

1

$input_line = 'Re: [Ticket #6588] Site security update' ; 
preg_match("/\[Ticket #(.*?)\]/i", $input_line, $output_array); 

echo $output_array[1]; 


/\[Ticket #(.*?)\]/i 

    \[ matches the character [ literally 
    Ticket # matches the characters Ticket # literally (case insensitive) 
    1st Capturing group (.*?) 
     .*? matches any character (except newline) 
      Quantifier: Between zero and unlimited times, as few times as possible, expanding as needed [lazy] 
    \] matches the character ] literally 
    i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z]) 
1
$string = 'Re: [Ticket #6588] Site security update'; 
preg_match('/\[Ticket #(.*?)\]/', $string, $matches); 
print_r($matches); 
관련 문제