preg_match_all('생략', $content, $match);
//print_r($match);
이렇게 하니 배열에 잘 담기는데여
다시한번
preg_match_all("/찾는문자/", $match, $matches2);
//echo "{$matches[0][0]} <br>";
//echo "{$matches[0][1]} <br>";
print_r($matches2[0]);
이렇게 하니 못찾습니다. 생각해보니
배열에 담겨서 그런거 같습니다.
이런때는 어떻게 할 수 있을까요?
찾는문자가 몇개인지 알고 싶습니다.
|
답변 4개 / 댓글 11개
채택된 답변
+20 포인트
결과물 찾고 그 안에서 또 찾는 것이라면 아래처럼 하세요.
Copy
preg_match_all('생략', $content, $match);
foreach ( $match[0] as $str ) {
preg_match_all("/찾는문자/", $str, $matches2);
print_r($matches2);
}
답변에 대한 댓글 8개
Array ( [0] => 번호 ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( [0] => 번호 ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( ) Array ( [0] => 번호 ) Array ( )
예를 들어 이렇게 3개가 나오는데
이걸 숫자로 3이라고 알고 싶습니다.
그래야 표를 뿌릴 수 잇어서요
예를 들어 이렇게 3개가 나오는데
이걸 숫자로 3이라고 알고 싶습니다.
그래야 표를 뿌릴 수 잇어서요
count($match[0]), count($matches2[0])로 배열의 크기를 구할 필요 없이
간단하게 앞에 변수 추가해주면 됩니다.
$total = preg_match_all('생략', $content, $match); // 처음 일치 개수
foreach ( $match[0] as $str ) {
$cnt = preg_match_all("/찾는문자/", $str, $matches2); // 두 번째 일치 개수
print_r($matches2);
}
+ preg_match_all()
- https://www.php.net/manual/en/function.preg-match-all.php
- http://docs.php.net/manual/kr/function.preg-match-all.php
간단하게 앞에 변수 추가해주면 됩니다.
$total = preg_match_all('생략', $content, $match); // 처음 일치 개수
foreach ( $match[0] as $str ) {
$cnt = preg_match_all("/찾는문자/", $str, $matches2); // 두 번째 일치 개수
print_r($matches2);
}
+ preg_match_all()
- https://www.php.net/manual/en/function.preg-match-all.php
- http://docs.php.net/manual/kr/function.preg-match-all.php
원래의 텍스트 내용(가릴 것 가리고, 필요한 최소 요소만 남기고),
원하는 결과물을 남기면 구할 수 있는 방법이 많겠는데
이런 식으로 남기면 본인이 더 많이 이해하고 풀어가야 합니다.
이렇게 남겨봐야 일부만 도움이 되겠죠. 원래의 데이터도 모르고, 원하는 결과물도 모르니까.
아무튼 이만 줄입니다.
Copy
<?php
$text = '<div class="a"><ul><li>1-1</li><li>1-2</li></ul></div>
<div class="a"><ul><li>2-1</li><li>2-2</li><li>2-3</li></ul></div>';
$total = preg_match_all('#<div class="a">.+?</div>#', $text, $matches);
echo '<table border="1">';
echo '<tr><td colspan="2">총 ', $total, '개 일치</td></tr>';
foreach ( $matches[0] as $str ) {
$cnt = preg_match_all('#<li>(.+?)</li>#', $str, $matches2);
echo '<tr><td rowspan="', $cnt, '">', htmlspecialchars($str, ENT_QUOTES), ' ... ', $cnt, '개 일치</td><td>';
foreach ( $matches2[1] as $k=>$val ) {
if ( !$k ) {
echo $val, '</td></tr>';
} else {
echo '<tr><td>', $val, '</td></tr>';
}
}
echo '</tr>';
}
echo '</table>';

몇 번 나타나는지 알려면
preg_match_all("/찾는문자/", $content, $matches2);
이렇게 해야 하는 거 아니가요?
배열을 문자열로 한줄로 만들어보세요
Copy
<?php
preg_match_all('생략', $content, $match);
//print_r($match);
$match = implode('|', $match);
preg_match_all("/찾는문자/", $match, $matches2);
//echo "{$matches[0][0]} <br>";
//echo "{$matches[0][1]} <br>";
print_r($matches2[0]);
?>
답변을 작성하려면 로그인이 필요합니다.