<p>Since you only have raw HTML in the redactor field and there is no real relationship between the asset in the craft and the img url, the only way to do this is to <a href="https://www.php.net/manual/en/domdocument.loadhtmlfile.php" rel="nofollow noreferrer">parse your readctor field</a> (or maybe even <a href="https://simplehtmldom.sourceforge.io/docs/1.9/index.html" rel="nofollow noreferrer">this</a>?), check the img tags, take the &quot;src&quot; part and check in the assets table (the last part of the path in the <em>src</em>).</p>
<pre><code>...&lt;img src=&quot;http://mysite.com/assetFolder/subfolder/**file_name.jpg**&quot;&gt;
</code></pre>
<p>And then find it like this:</p>
<pre><code>\craft\elements\Asset::find()-&gt;filename($parsedImgSrcValue);
</code></pre>

CC BY-SA 4.0
2023-03-14T23:41:03.503Z
Would it be possible to do this using the Preparse plug-in? Does Twig have a way to determine if the contents of the field contain a matching string? Something like regex? Per my example: if X contains "{string}"...then...

I don't have the PHP skills to use the method you describe.
4midori
CC BY-SA 4.0
2023-03-15T01:11:21.743Z
You can do {% if 'string' in block.fieldHandle ...' with twig, the in operator should work for checking the redactor contents.

https://twig.symfony.com/doc/3.x/templates.html#containment-operator
cherrykoda
CC BY-SA 4.0
2023-03-14T21:52:01.633Z
2023-03-14T21:52:01.633Z
RomanAvr
<p>If your end goal is simply to have a dynamic index/list of all entries where that field contains that string, you can use the new filtering options in the entries index list here:</p>
<p><a href="https://i.sstatic.net/2vHaR.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/2vHaR.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2023-03-16T13:37:10.940Z
Hmm, actually I've just noticed that this filter UI doesn't seem to include Redactor fields for some reason. Weird.
James Smith
CC BY-SA 4.0
2023-03-16T19:17:31.593Z
Alas, this is Craft 3... Good idea, however.
4midori
CC BY-SA 4.0
2023-03-16T12:42:17.613Z
2023-03-16T12:42:17.613Z
James Smith
<p>When an entry includes a reference to an asset, like an image, that information is tracked and displayed. (The included screenshot shows where that reference would be, although this particular entry does not have any related content.)</p>
<p>What I'm wondering is, is there a way to track asset references that occur within a Redactor field? Ideally, I could output this in the asset list view. Purpose: to clean up our asset library. We have tons of duplicates, but right now, there is no way to know if a given asset is referred to in a Redactor field.</p>
<p>Note: I use the Preparse plugin a lot, maybe there's a way to do something like:</p>
<pre><code>{% if element.myField contains '#asset' %}...{% endif %}
</code></pre>
<p>and have a column that indicates if there exist one or more asset references?</p>

CC BY-SA 4.0
2023-03-15T10:36:27.633Z
Not sure exactly what your end goal will be, but Retcon might help. https://github.com/mmikkel/Retcon-Craft
James Smith
<assets><relations><preparse>
Showing related assets referred to within Redactor (rich text) fields
# Showing related assets referred to within Redactor (rich text) fields

(Asked by: username_0 on 2023-03-09)

When an entry includes a reference to an asset, like an image, that information is tracked and displayed. (The included screenshot shows where that reference would be, although this particular entry does not have any related content.)
What I'm wondering is, is there a way to track asset references that occur within a Redactor field? Ideally, I could output this in the asset list view. Purpose: to clean up our asset library. We have tons of duplicates, but right now, there is no way to know if a given asset is referred to in a Redactor field.
Note: I use the Preparse plugin a lot, maybe there's a way to do something like:
{% if element.myField contains '#asset' %}...{% endif %}

and have a column that indicates if there exist one or more asset references?

**Tags:** assets, relations, preparse

> **Comments:**
> **username_1**: Not sure exactly what your end goal will be, but Retcon might help. https://github.com/mmikkel/Retcon-Craft  

---

## 2 answers

### Answer by username_2 on 2023-03-14. Score: 0 | Accepted answer

Since you only have raw HTML in the redactor field and there is no real relationship between the asset in the craft and the img url, the only way to do this is to parse your readctor field (or maybe even this?), check the img tags, take the "src" part and check in the assets table (the last part of the path in the src).
...

And then find it like this:
\craft\elements\Asset::find()->filename($parsedImgSrcValue);

> **Comments:**
> **username_0**: Would it be possible to do this using the Preparse plug-in? Does Twig have a way to determine if the contents of the field contain a matching string? Something like regex? Per my example: if X contains "{string}"...then...  I don't have the PHP skills to use the method you describe.  
> **username_3**: You can do {% if 'string' in block.fieldHandle ...' with twig, the in operator should work for checking the redactor contents.  https://twig.symfony.com/doc/3.x/templates.html#containment-operator  

---

### Answer by username_1 on 2023-03-16. Score: 0

If your end goal is simply to have a dynamic index/list of all entries where that field contains that string, you can use the new filtering options in the entries index list here:

[![enter image description here](https://i.sstatic.net/2vHaR.png)](https://i.sstatic.net/2vHaR.png)

> **Comments:**
> **username_1**: Hmm, actually I've just noticed that this filter UI doesn't seem to include Redactor fields for some reason. Weird.  
> **username_0**: Alas, this is Craft 3... Good idea, however.  

---

