我有一個完整的頁面HTML報廢,有很多標記包括HTML/CSS/JS代碼。下面正則表達式從廢棄的HTML提取Javascript對象
例(剝離量)
<p>blah blah blah html</p>
<script type="text/javascript">window._userData ={"country_code": "PK", "language_code": "en",user:[{"user": {"username": "johndoe", "follows":12,"biography":"blah blah blah","feedback_score":99}}],"another_var":"another value"} </script>
<script> //multiple script tags can be here... </script>
<p>blah blah blah html</p>
現在我要提取的對象中window._userData
,然後,如果可能的轉換所提取的字符串到PHP對象/陣列。
我已經嘗試了一些在SO上找到的正則表達式,但無法正常工作。
我也試圖在這裏類似的答案Regular expression extract a JavaScript variable in PHP
感謝
您想要提取的對象不正確。 – splash58
@ splash58我添加了缺少的},感謝您的評論,請解決任何問題? – Alyas
此外,它不能包含空格,並且必須包含引號中的所有鍵 - 「{」country_code「:」PK「,」language_code「:」en「,」user「:[{」user「:{」username「:」johndoe 「,」follow「:12,」biography「:」blah blah blah「,」feedback_score「:99}}],」another_var「:」另一個值「}' – splash58