0

I am stuck at one point and newbie at regex:

I need to parse a string in php and find out multiple values starting from particular pattern.

But that string which needs to be parsed is actually a javascript code.

This is the string (javascript code) that needs to be parsed :

<script src="http://192.168.1.172/jsScripts/jquery.min.js"></script>
<link rel="stylesheet" 
     type="text/css"  
     href="http://192.168.1.172/adqCss/adqExapandable.css">
<script src="http://192.168.1.172/jsScripts/adqExpandable.js"></script>
<script>
    var t1 = ${kom_demo_test1};
    var t2 = ${kom_demo_test2};
    var url = 'http://demo.com';
</script> 

Now i need to identify all words that start with ${kom_dom_ and then i want to store test1 , test2 in an array .

Any help is appreciated.

Thanks in advance :)

2
  • Will you parse that in PHP or Javascript? Commented Jan 16, 2014 at 11:36
  • I will parse it in PHP Commented Jan 16, 2014 at 11:58

2 Answers 2

1

You can do that quite easily using preg_match_all, something like this:

<?php
$input= '<script src="http://192.168.1.172/jsScripts/jquery.min.js"></script>
<link rel="stylesheet" 
     type="text/css"  
     href="http://192.168.1.172/adqCss/adqExapandable.css">
<script src="http://192.168.1.172/jsScripts/adqExpandable.js"></script>
<script>
    var t1 = ${kom_demo_test1};
    var t2 = ${kom_demo_test2};
    var url = \'http://demo.com\';
</script>';

$matches= array(); // An array to store the results
$results= preg_match_all('/\${kom_demo_(.*)}/', $input, $matches);

var_dump($matches);

Which will produce:

array
  0 => 
    array
      0 => string '${kom_demo_test1}' (length=17)
      1 => string '${kom_demo_test2}' (length=17)
  1 => 
    array
      0 => string 'test1' (length=5)
      1 => string 'test2' (length=5)

So $matches[1] contains the results you are looking for.

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

Comments

1

This is an expression that will work for this. Capture group 1 will give you everything after kom_demo_.

/\$\{kom_demo_([^\}]+)\}/

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.