PHP에서 문자열의 일부를 제거하려면 어떻게 해야 합니까?
문자열의 일부를 제거하려면 어떻게 해야 합니까?
문자열 예:"REGISTER 11223344 here"
어떻게 하면 제거할 수 있습니다."11223344"
위의 예제 문자열에서?
「1122344」를 대상으로 하고 있는 경우는, 다음을 사용합니다.
// str_replace($search, $replace, $subject)
echo str_replace("11223344", "","REGISTER 11223344 here");
str_replace()를 사용할 수 있습니다.이것은 다음과 같이 정의됩니다.
str_replace($search, $replace, $subject)
따라서 코드를 다음과 같이 쓸 수 있습니다.
$subject = 'REGISTER 11223344 here' ;
$search = '11223344' ;
$trimmed = str_replace($search, '', $subject) ;
echo $trimmed ;
정규 표현식을 통해 더 나은 조회가 필요한 경우 preg_replace()를 사용할 수 있습니다.
11223344가 일정하지 않다고 가정하면:
$string="REGISTER 11223344 here";
$s = explode(" ", $string);
unset($s[1]);
$s = implode(" ", $s);
print "$s\n";
str_replace(find, replace, string, count)
- find required(필수)를 선택합니다.찾을 값을 지정합니다.
- 교환이 필요합니다.find에서 값을 대체할 값을 지정합니다.
- 문자열이 필요합니다.검색할 문자열을 지정합니다.
- count 옵션.교체 횟수를 카운트하는 변수
OP 예시와 같이:
$Example_string = "REGISTER 11223344 here";
$Example_string_PART_REMOVED = str_replace('11223344', '', $Example_string);
// will leave you with "REGISTER here"
// finally - clean up potential double spaces, beginning spaces or end spaces that may have resulted from removing the unwanted string
$Example_string_COMPLETED = trim(str_replace(' ', ' ', $Example_string_PART_REMOVED));
// trim() will remove any potential leading and trailing spaces - the additional 'str_replace()' will remove any potential double spaces
// will leave you with "REGISTER here"
규칙 기반 조회가 필요한 경우 정규 표현을 사용해야 합니다.
$string = "REGISTER 11223344 here";
preg_match("/(\d+)/", $string, $match);
$number = $match[1];
이것은 첫 번째 숫자와 일치하기 때문에, 보다 구체적으로 할 필요가 있는 경우는, 다음과 같이 시험해 주세요.
$string = "REGISTER 11223344 here";
preg_match("/REGISTER (\d+) here/", $string, $match);
$number = $match[1];
subst()는 문자열의 일부를 반환하는 내장 PHP 함수입니다.substring() 함수는 문자열을 입력으로 사용하고 문자열을 잘라내는 인덱스 형식을 사용합니다.옵션 파라미터는 서브스트링의 길이입니다.기판에는 적절한 설명서와 예시 코드가 있습니다.
주의: 문자열 인덱스는 0으로 시작합니다.
동적으로 문자열의 (a) 고정 인덱스에서 (a) 부분을 삭제하려면 다음 함수를 사용합니다.
/**
* Removes index/indexes from a string, using a delimiter.
*
* @param string $string
* @param int|int[] $index An index, or a list of indexes to be removed from string.
* @param string $delimiter
* @return string
* @todo Note: For PHP versions lower than 7.0, remove scalar type hints (i.e. the
* types before each argument) and the return type.
*/
function removeFromString(string $string, $index, string $delimiter = " "): string
{
$stringParts = explode($delimiter, $string);
// Remove indexes from string parts
if (is_array($index)) {
foreach ($index as $i) {
unset($stringParts[(int)($i)]);
}
} else {
unset($stringParts[(int)($index)]);
}
// Join all parts together and return it
return implode($delimiter, $stringParts);
}
고객님의 목적:
remove_from_str("REGISTER 11223344 here", 1); // Output: REGISTER here
그 사용법 중 하나는 명령어와 같은 문자열을 실행하는 것입니다.이 문자열의 구조를 알고 있습니다.
다음 스니펫에 "여기에 등록"이 인쇄됩니다.
$string = "REGISTER 11223344 here";
$result = preg_replace(
array('/(\d+)/'),
array(''),
$string
);
print_r($result);
preg_replace() API의 사용방법은 다음과 같습니다.
$result = preg_replace(
array('/pattern1/', '/pattern2/'),
array('replace1', 'replace2'),
$input_string
);
다음을 수행합니다.
$string = 'REGISTER 11223344 here';
$content = preg_replace('/REGISTER(.*)here/','',$string);
그러면 "REGISTERhere"가 반환됩니다.
또는
$string = 'REGISTER 11223344 here';
$content = preg_replace('/REGISTER (.*) here/','',$string);
그러면 "여기에 등록"이 반환됩니다.
언급URL : https://stackoverflow.com/questions/2192170/how-can-i-remove-part-of-a-string-in-php
'IT' 카테고리의 다른 글
Larabel 5.4 및 Mariadb에서 너무 많은 연결 오류 발생 (0) | 2023.02.06 |
---|---|
PHP를 사용하여 오류 404 페이지를 만드는 방법 (0) | 2023.02.06 |
@see와 @inheritDoc의 차이점 상세 (0) | 2023.02.06 |
Ng-model이 컨트롤러 값을 업데이트하지 않음 (0) | 2023.02.06 |
x초마다 메서드/함수를 트리거하는 Vue js (0) | 2023.02.06 |