<p>You have to escape your commas, or craft wil interpret that as if your searching for several titles. Notice the where clause, "contenttitle1, contenttitle2, contenttitle3".</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;title = 'Title,with,commas';
$entry = $criteria-&gt;first();

// will result in this query:
SELECT &lt;a bunch of fields&gt;
FROM `craft_elements` `elements`
JOIN `craft_elements_i18n` `elements_i18n` ON elements_i18n.elementId = elements.id
JOIN `craft_content` `content` ON content.elementId = elements.id
...
WHERE (((((elements_i18n.locale = :locale) AND (content.locale = :locale)) AND (elements.archived = 0)) AND ((elements.enabled = 1) AND (elements_i18n.enabled = 1) AND (entries.postDate &lt;= '2015-05-21 09:08:27') AND ((entries.expiryDate is null) OR (entries.expiryDate &gt; '2015-05-21 09:08:27')))) AND ((content.title=:contenttitle1) OR (content.title=:contenttitle2) OR (content.title=:contenttitle3))) AND (elements_i18n.enabled = 1)
GROUP BY `elements`.`id`
ORDER BY `lft`, `postDate` DESC LIMIT 1. Bound with :locale='en', :contenttitle1='Title', :contenttitle2='with', :contenttitle3='commas')
</code></pre>

<p>Use this</p>

<pre><code>$criteria-&gt;title = 'Title\,with\,commas';
</code></pre>

CC BY-SA 3.0
2015-05-21T12:13:14.693Z
I thought this might have been the case - thanks!
crawf
CC BY-SA 3.0
2015-05-21T09:14:58.543Z
2015-05-21T09:14:58.543Z
fuling
<p>I'm not entirely sure why <code>$criteria-&gt;title</code> doesn't work (maybe someone can shed some light), but I did try these other two methods and they both pulled an entry with special characters like you describe above.</p>

<pre><code>$criteria-&gt;getContent()-&gt;title = 'Some$other$message';

$criteria-&gt;search = 'Some$other$message';`
</code></pre>

<p>I guess <code>$criteria-&gt;getContent()-&gt;title</code> is more explicit and more what you need, as search would match more than just the titles.</p>

CC BY-SA 3.0
2015-05-21T12:23:24.333Z
Thanks Alec - I would use search as its likely easier, but in my case, performance really matters, and its a lot slower than searching directly by handle.
crawf
CC BY-SA 3.0
2015-05-21T08:09:42.120Z
2015-05-21T08:09:42.120Z
Alec Ritson
<p>Have a look at <a href="http://buildwithcraft.com/classreference/helpers/DbHelper#parseParam-detail"><code>DbHelper::parseParam</code></a> docs:</p>

<blockquote>
  <p>If the $value is a string, it will automatically be converted to an array, split on any commas within the string (via ArrayHelper::stringToArray()). If that is not desired behavior, you can escape the comma with a backslash before it.</p>
</blockquote>

<p>To escape the comma and other characters treated as special in <a href="http://buildwithcraft.com/classreference/helpers/DbHelper#parseParam-detail"><code>DbHelper::parseParam</code></a> use the <a href="http://buildwithcraft.com/classreference/helpers/DbHelper#escapeParam-detail"><code>DbHelper::escapeParam</code></a> helper:</p>

<pre><code>$criteria-&gt;title = DbHelper::escapeParam("Title, with, commas");
</code></pre>

CC BY-SA 3.0
2015-05-21T12:13:43.173Z
Thats perfect @carlcs, exactly what I needed - nice one!
crawf
CC BY-SA 4.0
2020-07-01T12:03:33.923Z
Updating this answer for Craft 3: `use craft\helpers\Db; ... Db::escapeParam(...);`
James Smith
CC BY-SA 3.0
2015-05-21T09:53:38.207Z
2015-05-21T09:53:38.207Z
carlcs
<p>When trying to fetch an entry with special characters in the title, it doesn't seem to match.</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;title = "Title,with,commas";
$entry = $criteria-&gt;first();
</code></pre>

<p>I've also tried a few other characters such as <code>#</code>, <code>%</code>, and <code>$</code> with the same result - <code>$entry</code> is empty.</p>

<plugin-development><elementcriteriamodel>
Fetch entries with special characters in title
# Fetch entries with special characters in title

(Asked by: username_0 on 2015-05-21)

When trying to fetch an entry with special characters in the title, it doesn't seem to match.
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->title = "Title,with,commas";
$entry = $criteria->first();

I've also tried a few other characters such as #, %, and $ with the same result - $entry is empty.

**Tags:** plugin-development, elementcriteriamodel


---

## 3 answers

### Answer by username_3 on 2015-05-21. Score: 9 | Accepted answer

Have a look at DbHelper::parseParam docs:

If the $value is a string, it will automatically be converted to an array, split on any commas within the string (via ArrayHelper::stringToArray()). If that is not desired behavior, you can escape the comma with a backslash before it.

To escape the comma and other characters treated as special in DbHelper::parseParam use the DbHelper::escapeParam helper:
$criteria->title = DbHelper::escapeParam("Title, with, commas");

> **Comments:**
> **username_0**: Thats perfect @username_3, exactly what I needed - nice one!  
> **username_4**: Updating this answer for Craft 3: `use craft\helpers\Db; ... Db::escapeParam(...);`  

---

### Answer by username_1 on 2015-05-21. Score: 2

You have to escape your commas, or craft wil interpret that as if your searching for several titles. Notice the where clause, "contenttitle1, contenttitle2, contenttitle3".
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->title = 'Title,with,commas';
$entry = $criteria->first();

// will result in this query:
SELECT 
FROM `craft_elements` `elements`
JOIN `craft_elements_i18n` `elements_i18n` ON elements_i18n.elementId = elements.id
JOIN `craft_content` `content` ON content.elementId = elements.id
...
WHERE (((((elements_i18n.locale = :locale) AND (content.locale = :locale)) AND (elements.archived = 0)) AND ((elements.enabled = 1) AND (elements_i18n.enabled = 1) AND (entries.postDate <= '2015-05-21 09:08:27') AND ((entries.expiryDate is null) OR (entries.expiryDate > '2015-05-21 09:08:27')))) AND ((content.title=:contenttitle1) OR (content.title=:contenttitle2) OR (content.title=:contenttitle3))) AND (elements_i18n.enabled = 1)
GROUP BY `elements`.`id`
ORDER BY `lft`, `postDate` DESC LIMIT 1. Bound with :locale='en', :contenttitle1='Title', :contenttitle2='with', :contenttitle3='commas')

Use this
$criteria->title = 'Title\,with\,commas';

> **Comments:**
> **username_0**: I thought this might have been the case - thanks!  

---

### Answer by username_2 on 2015-05-21. Score: 1

I'm not entirely sure why $criteria->title doesn't work (maybe someone can shed some light), but I did try these other two methods and they both pulled an entry with special characters like you describe above.
$criteria->getContent()->title = 'Some$other$message';

$criteria->search = 'Some$other$message';`

I guess $criteria->getContent()->title is more explicit and more what you need, as search would match more than just the titles.

> **Comments:**
> **username_0**: Thanks Alec - I would use search as its likely easier, but in my case, performance really matters, and its a lot slower than searching directly by handle.  

---

