2014-01-05 2 views
0

2 개의 기능을 썼습니다. 첫 번째는 다음과 같이이다 :
첫 번째 일치에서만 작동하는 preg_replace

function Find($string, $first, $last) 
{ 
    $string = " ".$string; 
    $action = strpos($string,$first); 
    if ($action == 0) return ""; 
    $action += strlen($first);  
    $result = strpos($string,$last,$action) - $action; 
    return substr($string,$action,$result); 
} 

그리고 여기에 두 번째 :
나는 그것이 #*로 시작하고 *#로 끝나는 문자열을 대체 할

function Replace($string) 
{ 
    $find = Find($string, '#*','*#'); 
    return preg_replace('/'.$find.'/', '___', $string); 
} 

. 다음은 예제 텍스트입니다. Donec gravida #*c_01*# quam nec pulvinar #*c_02*# facilisis. Donec sed consectetur #*c_03*# lectus.

다음과 같은 함수를 사용할 때 : echo Replace($string); 첫 번째 문자열 만 바꿉니다. 좋아요 : Donec gravida #*___*# quam nec pulvinar #*c_02*# facilisis. Donec sed consectetur #*c_03*# lectus.

도와 주시겠습니까? 어떻게 관리하나요?

+0

http://us2.php.net/manual/en/function.preg-replace.php – bucabay

+0

그래, 내가 읽은 문서와 처음 엔으로 시도 @ Session의 사용법과 같은 리미터. 하지만 결과는 동일합니다 : 좌절 ... –

+0

당신의 정규식에 글로벌 플래그를 추가 하시겠습니까? '/ foo/g' – rlemon

답변

0

preg_replace의 한계 매개 변수를 설정해보십시오. 예를 들어

변환 :

preg_replace('/'.$find.'/', '___', $string, 1); 
+0

먼저 @ Session의 사용법과 같은 리미터로 시도했습니다. 그러나 결과는 동일합니다. 좌절감 ... –

0

난 당신이 다음 마커 (#**#)의 한 쌍의 사이의 값을 추출 또 다른 형태로 포맷을 할 생각

Donec gravida #*c_01*# quam nec pulvinar #*c_02*# facilisis. 
Donec sed consectetur #*c_03*# lectus 

Donec gravida link=?c_01 quam nec pulvinar link=?c_02 facilisis. 
Donec sed consectetur link=?c_03 lectus 

그렇다면 일반 ex에서 그룹 캡처를 사용할 수 있습니다. Pression의 : 정규 표현식은 #**# 사이의 값을 추출합니다

/(?:#\*)+([^#\*]+)+(?:\*#)/ 

.

<?php 
$str = 'Donec gravida #*c_01*# quam nec pulvinar #*c_02*# facilisis. Donec sed consectetur #*c_03*# lectus'; 
$matches = array(); 
$ptn = '/(?:#\*)+([^#\*]+)+(?:\*#)/'; 
preg_match_all($ptn, $str, $matches); 
var_export($matches); 

출력 될 것이다

array (
    0 => 
    array (
    0 => '#*c_01*#', 
    1 => '#*c_02*#', 
    2 => '#*c_03*#', 
), 
    1 => 
    array (
    0 => 'c_01', 
    1 => 'c_02', 
    2 => 'c_03', 
), 
) 

$matches[0] 전체 패턴과 일치하는 텍스트를 포함하는 것, $matches[1]는 제 캡처 괄호 서브 패턴과 일치하는 텍스트를 가질 것이다

따라서 preg_replace()을 사용하여 변환하십시오.

<?php 
$str = 'Donec gravida #*c_01*# quam nec pulvinar #*c_02*# facilisis. Donec sed consectetur #*c_03*# lectus'; 
$matches = array(); 
$ptn = '/(?:#\*)+([^#\*]+)+(?:\*#)/'; 
$str = preg_replace($ptn, 'link?=$1', $str); 
echo $str, "\n" 

출력 될 것이다

Donec gravida link?=c_01 quam nec pulvinar link?=c_02 facilisis. 
Donec sed consectetur link?=c_03 lectus 

preg_replace가 두 번째 인자() 고지 해주세요, 그것은 $matches[1]이다.

+0

설명은 종종 단순한 코드보다 도움이됩니다. – rlemon

+0

이것은 좋은 해결책이지만 사실이 두 함수는 사용 된 생성 동적 링크를 생성합니다. 나는 "c"와 "01,02,03"값을 얻을 수 있었다. 따라서 사이트가 mod_rewrite를 사용하도록 설정하면 http://sitenme.com/c_01-title-1.html, http://sitenme.com/c_02-title-2.html 및 http : // sitenme가 생성됩니다. .com/c_03-title-3.html. 하지만 링크는 http://sitenme.com/comment.php?id=1, http://sitenme.com/comment.php?id=2 및 http://sitenme.com/comment.php에서 생성됩니다. ? id = 3

그래서 위의 2 가지 함수를 사용해야한다고 생각합니다. –

+0

@JimJerry, 'sitenme.com/c_01-title-1.html, sitenme.com/c_02-title-2.html'을'sitenme.com/comment.php?id=1, sitenme '으로 변환 하시겠습니까? com/comment.php? id = 2'? – srain

0

다음은 멋진 재활용 기능입니다. 단순히 문자열을 전달하여 '있는 그대로'사용할 수도 있고, 바꾸기와 패턴을 전달할 수도 있습니다.

function ReplaceChar($string, $replace = "#*___*#", $pattern = "/\#\*(.*?)\*\#/"){ 

    $new = preg_replace($pattern, $replace, $string); 

    return $new; 

} 

$string = "Donec gravida #*___*# quam nec pulvinar #*c_02*# facilisis. Donec sed consectetur #*c_03*# lectus."; 

echo ReplaceChar($string); 

이 발생합니다 :

Donec gravida #*___*# quam nec pulvinar #*___*# facilisis. Donec sed consectetur #*___*# lectus. 
관련 문제