1

I have predefined an array:

$tags = array('PHP', 'Webdesign', 'Wordpress', 'Drupal', 'SQL');

Now I am inputting a value into a text area:

$text = 'Working With Wordpress Shortcodes and doing some NoSQL and SQL';

How can I compare the string value with the predefined array?

The desired result I want based on the above is 'Wordpress' and 'SQL'

Can I do it in PHP and JQuery?

Also, if it will contain a regular expression like

Working With Wordpress; Shortcodes: and doing some NoSQL and ""SQL

Then what to do?

1

4 Answers 4

2

You should split your string like so

$array = explode( ' ', $text );

Then compare like so

$result_array = array_intersect($tags, $array);

Now result_array will contain every value that is in both of the arrays :)

Sign up to request clarification or add additional context in comments.

1 Comment

if it will contains with regular expression like === Working With Wordpress; Shortcodes: and doing some NoSQL and ""SQL == then what to do?
0

Using explode split the string to works, than for each word check in_array

$words = explode(' ', $text);
$result = array();
foreach ($words as $word) {
   if (in_array($word, $tags)) {
      $result[] = $word;
   }
}

Note: the comparision will be case-sensitivive

1 Comment

if it will contains with regular expression like === Working With Wordpress; Shortcodes: and doing some NoSQL and ""SQL == then what to do?
0
foreach(explode(' ', $text) as $word) {
  if(in_array($word, $tags)) {
    // This word is in our tags array, do something.
  }
}

2 Comments

if it will contains with regular expression like === Working With Wordpress; Shortcodes: and doing some NoSQL and ""SQL == then what to do?
Sorry I don't understand what you mean?
0

i have found some solution and it works perfectly for me. Firstly i am rearranged the string with a "," separated using

var valsp = $('#desc').val(); var results = valsp.split(/\W+/);

after this just using inArray() i am getting the desired result

Hope it will help to others

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.