<p>I had the exact same issue with a client and their horrendous Wordpress blog posts.</p>

<p>In the end, after importing all the posts and copying over the related images, I wrote a simple plugin that lets you use a <code>|firstImage</code> filter to grab the first image from a rich text field. I've added it to GitHub: <a href="https://github.com/DigitalSquid/Craft-CMS-First-Image" rel="nofollow noreferrer">https://github.com/DigitalSquid/Craft-CMS-First-Image</a></p>

<p>A rough example of how to use it:<code>
{% set firstImage = entry.body|firstImage %}
{% if firstImage|length %}
    &lt;img src="{{ firstImage }}" /&gt;
{% endif %}</code></p>

CC BY-SA 3.0
2017-09-30T23:51:59.513Z
2017-10-01T00:23:52.393Z
2017-10-01T00:23:52.393Z
Digital Squid
<p>Sounds like a job for <a href="https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/" rel="nofollow noreferrer">regular expressions</a>.</p>

<p>Twig has limited support for regex natively, but there is a handy Craft <a href="https://github.com/victor-in/Craft-TwigPCRE" rel="nofollow noreferrer">plugin</a> that adds <a href="https://secure.php.net/manual/en/function.preg-match.php" rel="nofollow noreferrer">preg_match</a> support, which is what you'll need.</p>

CC BY-SA 3.0
2017-08-31T17:11:19.533Z
2017-08-31T17:11:19.533Z
Brad Bell
<p>I've had the unfortunate task of importing thousands of entries in to Craft from Worpdress. </p>

<p>Each of the entries has a Rich Text field and within the first couple of paragraphs there is an image.</p>

<p>I need to use this image as the overview thumbnail on a listing page. How can I extract that image tag, or just the src attribute from it to get the url.</p>

<p>Example of the image tag</p>

<pre><code>&lt;img class="size-full wp-image-13325" src="https://www.domain.com/news/wp-content/uploads/2015/12/image.jpg" alt="etc" width="620" height="385" /&gt;
</code></pre>

<p>I have tried to use the <code>|split</code> filter and some others with no success.</p>

<p>Thanks in advance for any help/direction</p>

<templating><search><filter>
Grab first <img> or <img> src from Rich Text field
# Grab first <img> or <img> src from Rich Text field

(Asked by: username_0 on 2017-08-31)

I've had the unfortunate task of importing thousands of entries in to Craft from Worpdress. 
Each of the entries has a Rich Text field and within the first couple of paragraphs there is an image.
I need to use this image as the overview thumbnail on a listing page. How can I extract that image tag, or just the src attribute from it to get the url.
Example of the image tag


I have tried to use the |split filter and some others with no success.
Thanks in advance for any help/direction

**Tags:** templating, search, filter


---

## 2 answers

### Answer by username_1 on 2017-09-30. Score: 2

I had the exact same issue with a client and their horrendous Wordpress blog posts.

In the end, after importing all the posts and copying over the related images, I wrote a simple plugin that lets you use a `|firstImage` filter to grab the first image from a rich text field. I've added it to GitHub: <https://github.com/DigitalSquid/Craft-CMS-First-Image>

A rough example of how to use it:`{% set firstImage = entry.body|firstImage %}
{% if firstImage|length %}
![]({{ firstImage }})
{% endif %}`


---

### Answer by username_2 on 2017-08-31. Score: 0

Sounds like a job for [regular expressions](https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/).

Twig has limited support for regex natively, but there is a handy Craft [plugin](https://github.com/victor-in/Craft-TwigPCRE) that adds [preg\_match](https://secure.php.net/manual/en/function.preg-match.php) support, which is what you'll need.


---

