0

I am currently trying to extract a numeric value from an html string. I am currently using preg_match but to no luck. All I desire is to get the numeric value set as param of a javascript function myFunction. As of now I get an empty array. Any help to achieve this?

$data = "<th style='text-align:center;width:33%;'><a onclick='return myfunction(12314568);' href='#test' class='btn mini full'>Test</a> </th>";

preg_match("/\[return myfunction([0-9]+)\]/", $data, $m);
print_r($m);
2
  • 3
    first off, ( and ) have special meaning in regex, so you must escape them. second, where the heck are you getting the [ and ] from? if you remove those, and escape the () , your regex works fine. 3v4l.org/8qvnH Commented Apr 13, 2018 at 19:37
  • @hanshenrik ahh you are absolutely right. Overlooked those two things. Commented Apr 13, 2018 at 19:38

1 Answer 1

1

The brackets need to be removed and the parentheses need to be escaped since they actually appear in the string. You'll then need another set of parentheses to group the numbers so they are separated:

preg_match("/return myfunction\(([0-9]+)\)/", $data, $m);
Sign up to request clarification or add additional context in comments.

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.