- imagecreatefromstring
- Список параметров
- Возвращаемые значения
- Примеры
- Смотрите также
- Php turn image to string php
- How to remove first image from string
- Remove image from string based on src url
- Remove ‘img’ tag from string using preg_replace [duplicate]
- PHP — How to replace empty alt tag on image
- php — Convert image to string without saving image
- Solution:
- Share solution ↓
- Additional Information:
- Didn’t find the answer?
- Similar questions
- Write quick answer
- About the technologies asked in this question
- PHP
- Welcome to programmierfrage.com
- Get answers to specific questions
- Help Others Solve Their Issues
imagecreatefromstring
imagecreatefromstring() возвращает идентификатор изображения, представляющего изображение полученное из потока image . Эти типы будут автоматически определяться, если сборка PHP их поддерживает: JPEG, PNG, GIF, WBMP и GD2.
Список параметров
Строка содержащая данные изображения.
Возвращаемые значения
В случае успеха будет возвращен ресурс изображения, FALSE , если тип изображения не поддерживается, данные не распознаются или данные нарушены и не могут быть загружены.
Примеры
Пример #1 Пример использования imagecreatefromstring()
$data = ‘iVBORw0KGgoAAAANSUhEUgAAABwAAAASCAMAAAB/2U7WAAAABl’
. ‘BMVEUAAAD///+l2Z/dAAAASUlEQVR4XqWQUQoAIAxC2/0vXZDr’
. ‘EX4IJTRkb7lobNUStXsB0jIXIAMSsQnWlsV+wULF4Avk9fLq2r’
. ‘8a5HSE35Q3eO2XP1A1wQkZSgETvDtKdQAAAABJRU5ErkJggg==’ ;
$data = base64_decode ( $data );
?php
$im = imagecreatefromstring ( $data );
if ( $im !== false ) header ( ‘Content-Type: image/png’ );
imagepng ( $im );
imagedestroy ( $im );
>
else echo ‘Произошла ошибка.’ ;
>
?>
Результатом выполнения данного примера будет что-то подобное:
Смотрите также
- imagecreatefromjpeg() — Создает новое изображение из файла или URL
- imagecreatefrompng() — Создает новое изображение из файла или URL
- imagecreatefromgif() — Создает новое изображение из файла или URL
- imagecreatetruecolor() — Создание нового полноцветного изображения
Php turn image to string php
The output will be: Solution 3: if it is valid XML, you can use about any decent XML parser (SimpleXML for example), unmarshall the source, traverse the tree looking for image tags, try lookup the attribute and set it if it does not exist, and the remarshall the xml . should be a 10-liner or so . good luck . Solution 2: try: Solution 3: Replace this regex with empty string Question: Current image looks like Now if do not have the , the code itself will replace the image like How can be done with php? Solution 1: The quickest solution, assuming images always follow that format, is this: However, I would question your motives.
How to remove first image from string
I have the results from a database that contains images and text. I would like to remove the first image.
$string = 'This is some text and images
, more text and and so on';
I would like to remove the first image, it is not always the same url.
$string = 'This is some text and images
, more text and and so on'; print preg_replace('//i','',$string,1);
This is some text and images , more text and
and so on
Assuming you know it’ll be prefixed by spaces and a line break, and suffixed by a comma and line break (and you want to remove these, too), you can do
print preg_replace("/\n \,\n /i",'',$string,1);
This is some text and images more text and
and so on
There was a great answer on another Thread
function get_first_image($html)< require_once('SimpleHTML.class.php') $post_dom = str_get_dom($html); $first_img = $post_dom->find('img', 0); if($first_img !== null) < return $first_img->src; > return null; >
You can do it via Regex expressions however regex isn’t really suited for this.
$var = 'This is some text and images
, more text and and so on'; echo preg_replace('//', '123', $var, 1);
This should do it. ? in the regex is to make it ungreedy.
Being a RegEx newbie, I tend to avoid it and use something like:
$i = strpos($string,'
', $i); if ($j !== false) $string = substr($string,0,$i) . substr($string,$j); >
Probably should be $i-1 and/or $j+1 — I never remember exactly what it should be.
Php — Remove image from string based on src url, I am looking for an easy and efficient way to remove a specific image from an article. All that I know is the image URL of the image that I need to remove. The image may or may not use different attributes. The image may or may not exist at all in the article. There might be other images (not same url) in the article.
Remove image from string based on src url
I am looking for an easy and efficient way to remove a specific image from an article. All that I know is the image URL of the image that I need to remove.
- The image may or may not use different attributes.
- The image may or may not exist at all in the article.
- There might be other images (not same url) in the article.
My choice would be either regex or DOMDocument , probably using an HTML5 parser like https://github.com/Masterminds/html5-php.
My regex skills are not that good, and I’m not sure if it’s a good idea to use regex to accomplish this because I read that regex should be avoided to parse HTML. What I have with so far with regex, is to remove the complete image, but not sure how to remove it based on a specific src url.
$img_src = 'http://www.example.org/image_to_be_removed.jpg'; $article = 'Test article with HTML5 tags
This is an example article. The article may or may not include html5 tags, images and other things.

More example text.
'; $article = preg_replace("/
]+\>/i", "", $article); echo $article;
I haven’t dug into the DOMDocument solution yet, because I am not sure if it’s even possible or if regex might be considered best practice?
$article = preg_replace("/
]+src=\"" . preg_quote($img_src, '/') . "\"[^>]*\>/i", "", $article);
You can try this. It seems to test ok. At any rate it should give you an idea as to how to proceed.
$img_src = 'http://www.example.org/image_to_be_removed.jpg'; $article = 'Test article with HTML5 tags
This is an example article. The article may or may not include html5 tags, images and other things.
\s/'; //Define the regex. $article = preg_replace($regex, '', $article); echo $article;
You can try below with str_replace
Test article with HTML5 tags This is an example article. The article may or may not include html5 tags, images and other things.

More example text.
'; $new = str_replace('src="http://www.example.org/image_to_be_removed.jpg"','',$article); echo $article; echo '
'; echo $new; ?>
there is both preg_replace from your code and str_replace,to notice deference. There are other function to do the same like sprintf,strtr,str_replace and preg_replace you can use whichever suites
It is not recommended to parse html with regex.
As you suggested, you might for example use DOMDocument or for example PHP Simple HTML DOM Parser.
Because you state that «All that I know is the image URL of the image that I need to remove», you could find the src attribute of the img tag using xpath or looking for the tag name and check that.
$img_src = 'http://www.example.org/image_to_be_removed.jpg'; $article = 'Test article with HTML5 tags
This is an example article. The article may or may not include html5 tags, images and other things.


More example text.
\'; More example text.
'; $dom = new DOMDocument(); libxml_use_internal_errors(true); $dom->loadHTML($article); $xpath = new DOMXPath($dom); $elements = $xpath->query("//img"); foreach ($elements as $elememnt) < if ($elememnt->getAttribute("src") === $img_src) < $elememnt->parentNode->removeChild($elememnt); > > echo $dom->saveHTML();
Example PHP Simple HTML DOM Parser using simple_html_dom.php :
$htmlDom = str_get_html($article); foreach($htmlDom ->find('img[src=http://www.example.org/image_to_be_removed.jpg]') as $item) < $item->outertext = ''; > $htmlDom->save(); echo $htmlDom;
PHP Object to String | How to Convert Object to String in, There is no exact syntax which is extensively used to convert PHP object into String but there are other ways to achieve it for those conversions, syntax exist and are used by embedding them within the code which is represented as follows : $ var = some_name $var = obj_1; < Call function with $var; Use _toString () method; >…
Remove ‘img’ tag from string using preg_replace [duplicate]
Possible Duplicate:
PHP — removetag from string
I have my content like this:
$content = '

An explorer adventures into an unknown world, yet it seems that he has been there before. A short animated film directed by Malcolm Sutherland in 2010. With music by Alison Melville and Ben Grossman, and foley by Leon Lo. Sound design / mix by Malcolm Sutherland.
The animation is all hand-drawn; a mix of drawing / pastels on paper and digital animation with Toonboom Studio and a wacom cintiq tablet, assembled in After Effects 7 and edited in Sony Vegas 8.
';
I want the output ignoring the tag. I tried some messups with preg_replace but didn’t work.. It will be a great help if someone can explain me how it works.
If you are not forced to use regular expressions to parse HTML content, then I’d suggest using PHP’s strip_tags() function along with its allowed_tags argument.
This will tell PHP to remove all the html tags and leave the ones your specified in the allowed_tags .
Example usage taken from the documentation —
$text = 'Test paragraph.
Other text'; echo strip_tags($text); // output : Test paragraph. Other text // Allow and echo strip_tags($text, '
'); // output -
Test paragraph.
Other text
So, if you simply specify all the HTML tags in allow_tags except the tag , you should get the results that you need — only removing the
tag from your string.
$content = preg_replace("/
]+\>/i", " ", $content);
Replace this regex )|()) with empty string
How to convert a PHP object to a string?, Catchable fatal error: Object of class stdClass could not be converted to string So, my question is, how do I convert an object to a string in PHP? I don’t want to serialize it though. Just a note: the code I use works in PHP 4, but not in PHP 5. Thanks! EDIT: I resolved it myself. It was a pain, but I did it. Thanks …
PHP — How to replace empty alt tag on image
Now if img src do not have the alt=»» , the code itself will replace the image like
The quickest solution, assuming images always follow that format, is this:
$search = '/
/'; $replace = '
'; $code = preg_replace( $search, $replace, $code );
However, I would question your motives. Adding a blank alt tag is pretty pointless.
Here’s an example of doing this with SimpleXML.