Solved

display image with preg_replace

Posted on 2014-01-07
1
166 Views
Last Modified: 2014-01-07
hello,
I have this code that displays
hello (image) world
I would like to just display the image.. "hello world" is just an example and it needs to use regex because that text always changes

function display_image($content)
{
	$pattern = '/(<img [^>]*>)/i';
	$replacement = '<a href="http://google.com"">' . '\1</a>';
	$content = preg_replace($pattern, $replacement, $content);
	return $content;
}

$textes = 'hello <img src="https://www.google.com/images/errors/logo_sm.gif"/> world';
echo display_image($textes);

Open in new window

0
Comment
Question by:XK8ER
1 Comment
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39763019
function display_image($content)
{
	preg_match('/(<img.*?>)/', $content, $result);
	return $result[1];
}

Open in new window

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now