php

php 이미지태그 없애기

xemaker 2022. 2. 11. 13:42

html의 img 태그를 없애야 할 일이 생겼다.

흠.. 어떻게 없애지..

구글링 해보니 결론적으로는 아래가 답인데.. 

preg_replace("/<img[^>]+\>/i", "", $content);

https://stackoverflow.com/questions/1107194/php-remove-img-tag-from-string

 

PHP - remove <img> tag from string

Hey, I need to delete all images from a string and I just can't find the right way to do it. Here is what I tryed, but it doesn't work: preg_replace("/<img[^>]+\>/i", "(image) ", $conten...

stackoverflow.com

 

stackoverflow 질문처럼 해도 안되는 경우가 있다. 이런 실수를 많이 하게 되는데..

php 매뉴얼을 보면

preg_replace() returns an array if the subject parameter is an array, or a string otherwise.

preg_replace()는 returns 즉, 리턴한다고 써있다.

그냥 저렇게만 쓰면 리턴한 값을 받는 부분이 없으니 당연히 안된다.

리턴한 값을 받아줘야 한다.

그래서 

$content=preg_replace("/<img[^>]+\>/i", "", $content);

이렇게 받아줘야 한다.