Solved

display image with preg_replace

Posted on 2014-01-07
1
173 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to dynamically set the form action using jQuery.

726 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