Roberto Marras
Roberto Marras

Reputation: 153

Check if a variable is a specific string other wise do that

To check if a field has or not a string i simply do:

if( $mindmap !== 'hello' ) {...

That is fine however the field has a "special" string, exactly the following:

$mindmap = "{"id":1}";

I tried:

if( $mindmap !== '{""};' ) {... 

and

 if( $mindmap !== '{"id":1}' ) {...

But that doesn't work and I don't know why to be honest, any idea?

Upvotes: 0

Views: 43

Answers (2)

Hanky Panky
Hanky Panky

Reputation: 46900

Based on our discussion in comments it looks like that the input string sometimes has html entities in it. Here is a proof of concept that you can build upon, it decodes any possible entities within the string before comparing it with a standard one.

<?php

$mindmap = "{&quot;id&quot;:1}";
var_dump( $mindmap === '{"id":1}');                         //false
var_dump( html_entity_decode($mindmap) === '{"id":1}');     //true

Fiddle

Your if would look like

if( html_entity_decode($mindmap) !== '{"id":1}')

Upvotes: 2

Sunny Kumar
Sunny Kumar

Reputation: 836

$mindmap = "{&quot;id&quot;:1}";

echo "<pre>";
var_dump($mindmap !== "{&quot;id&quot;:1}"); exit; //bool(false)
var_dump($mindmap === "{&quot;id&quot;:1}"); exit; //bool(true)

Works perfectly...

what you need from that????

Upvotes: 0

Related Questions