2014-03-04 3 views
-1

문자열의 특정 부분을 추출하는 방법을 알려주십시오. 그들은 일반적으로 다음과 같이 :문자열에서 특정 텍스트를 추출하는 PHP/MySQL

Includes USD39 bundled airtime, 30 SMSs and 50MB data. 
Includes USD200 bundled airtime, 110 SMSs and 450MB data. 
Includes USD399 bundled airtime, 500 SMSs and 650MB data. 

모든 난을하려면 각각의 문자열에서

USD39 bundled airtime 
USD200 bundled airtime 
USD399 bundled airtime 

조각을 표시입니다 싶습니다. 사과 그래서이 여기에 꽤 많은 사람에게 고통스럽게 쉬운 것입니다 알고

, : D 잘하면 내가 더 도전적인 질문 일일 요청할 수 있습니다 .. FROM

+3

시도한 내용을 게시 할 수 있습니까? –

+1

try preg_replace ("/ Includes (. *),. * /", "$ 1", $ str); – Nambi

+0

왜이 질문은 downvoted beeing입니까? – 2cent

답변

0
SELECT SUBSTRING(sentence,LOCATE(' ',sentence),LOCATE(' ',sentence,(LOCATE(' ',sentence)+1))-LOCATE(' ',sentence)) 

(SELECT 'USD39 번들 방송, (30) SMS 및 50MB 데이터 '문장)

이 항목은 USD39를 추출해야하지만 확인되지는 않았습니다. 세 번째와 네 번째 값이 각 진술에서 동일하므로 다른 USD 값을 얻기를 바랍니다.

희망이 도움이됩니다!

+0

Mysql 쿼리 자체 대신에 PHP 코드 안에 로직을 유지하는 것이 좋습니다. –

1

정규식 바위!

$str = 'Includes USD39 bundled airtime, 30 SMSs and 50MB data.'; 
$matches = array(); 
preg_match('/ USD\d+ bundled airtime/i', $str, $matches); 
echo '<pre>', print_r($matches, 1), '</pre>'; 

// more complex: 
$matches = array(); 
preg_match('/ (USD\d+ [^,]+),/i', $str, $matches); 
echo '<pre>', print_r($matches, 1), '</pre>'; 

// another way: 
$matches = array(); 
preg_match('/^Includes ([^,]+),/i', $str, $matches); 
echo '<pre>', print_r($matches, 1), '</pre>'; 
-1

echo substr("Hello world",1,8); 

이 유사하게 답변을 공식화하고보십시오. (이것은 PHP 함수입니다. 도움이 되길 바랍니다.) 그렇지 않으면 정규 표현식이 핵심이됩니다.

+0

이것은 OP가 원하는 것이 아닙니다. –

관련 문제