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}
It looks like JSON, but it isn\'t : it\'s lacking all the commas (\',\') and all the colons (\":\"). I\'m trying to find a way to parse it and turn into a valid JSON string.
My guess is to use the tabulations and the line breaks to find my way in the string, but so far I wasn\'t able to convert it to valid JSON.
I came up with this, which works but I believe is not very efficient or fail safe :
$e = explode( \"\", $string );
foreach( $e as $k => $l ) {
$next = str_replace( \"\\t\", \'\', $e[ $k + 1 ] );
$isParam = strstr( $l, \"\\\"\\t\\t\\\"\" );
$currentClean = str_replace( \"\\t\", \'\', $l );
if ( $isParam )
$e[ $k ] = str_replace( \"\\\"\\t\\t\\\"\", \'\":\"\', $l );
if ( $isParam && $next != \'}\' )
$e[ $k ] .= \',\';
if ( $currentClean == \'}\' && $next != \'}\' && $next )
$e[ $k ] .= \',\';
if ( preg_match( \'#^\"(.*)\"$#\', $currentClean ) && !$isParam )
$e[ $k ] .= \':\';
}
$json = implode(\"
\", $e);
How to improve this ?
The problem is that you have several replaces at hand...so my answer comes with several calls (i try with all the text, it doesn´t need to be separated each line, just enter the full text).
//For the \\\":\\\" before \\\"}\\\" and keys$alltext = preg_replace(\\\"/(\\\\\"\\w+\\\\\")\\s+(\\{|(\\\\\"\\w*\\\\\"))/\\\", \\\"$1:$2\\\", $alltext);
//For \\\",\\\" at the ends of the values:keys
$alltext = preg_replace(\\\"/(\\\\\"\\w*\\\\\":\\\\\"\\w*\\\\\")(?!\\s+(\\}|\\{))/\\\", \\\"$1$2,\\\", $alltext);
//For the \\\",\\\" at the end of each \\\"}\\\" that has a value after
$alltext = preg_replace(\\\"/(\\})(\\s+\\\\\")/\\\", \\\"$1,$2\\\", $alltext);
PD: Dont know what your values are so thats why I left the \\w+ in the answer. Let me know if it was helpful.
PD2:You can try it at phpliveregex
Not completely perfect but a really good start:
// Replace \'\\\"xxx\\\" \\\"xxx\\\"\' by \'\\\"xxx\\\": \\\"xxx\\\",\'$string = preg_replace(\'/(\\\"[^\\\"]+\\\")\\s+(\\\"[^\\\"]*\\\")/\',\'$1: $2,\',$string);
// Replace \'} \\\"xxx\\\" {\' with \\\"} \\\"xxx\\\": {\\\", accept \\\"}\\\" and \\\"{\\\" for start/end
$string = preg_replace(\'/([{}])(\\s+)(\\\"[^\\\"]+\\\")\\s+([{}])/\',\'$1$2$3:$4\',$string);
// Replace \\\", {\\\" by \\\" {\\\"
$string = preg_replace(\'/,(\\s*[}{])/\',\'$1\',$string);
echo $string;
Problem: There is a ,
placed after each match by first regex. Should not be the case for the last value in a list of values as here:
...\\\"XXX\\\":{
\\\"XXX\\\": \\\"XXX\\\",
\\\"XXX\\\": \\\"\\\", <-- Wrong!
}
...
Working code example can be found at http://sandbox.onlinephpfunctions.com/code/36f618bbeaaa861a88b873144e25232c5ba2c8e1
浏览器相关的对象。获取浏览器相关的信息,可以设置和修改浏览器属性。
window
浏览器相关的宽高信息
setInterval
setTimeout
clearInterval
clearTimeout
fetch:未来学习ajax的时候可以用到的方法
open:打开一个新的页面
outerHeight: 浏览器的高度
outerWidth: 浏览器的宽度
alert:仅仅只是一个弹框,只有一个确定按钮
comfirm:有确定和取消按钮的弹框,返回值分别为true和false
prompt:这是一个可以让用户输入内容的弹框。(不建议使用)
scrollto:设置滚动条,滚动到什么位置,语法:scrollTo(水平位置,垂直位置)
2.1 延迟函数:setTimeout
延迟一段时间执行某个函数,setTimeout有返回值,这个返回值即是setTimeout的id值。
注意:延迟函数是异步执行的。
语法1:setTimeout(函数对象,延迟多少毫秒执行)
语法2:setTimeout(函数对象,延迟时间,后面的参数皆为函数对象的参数)
清除延迟函数:clearTimeout
语法:clearTimeout(延迟函数的ID)
2.2 间隔函数:setInterval
每隔一段时间执行一次,第一次执行也会延迟。间隔函数也是异步执行函数,会将间隔执行的函数对象,放置到内存的事件队列里,到了时间点,就会从事件队列拿到主线程进行执行,主线程会根据在空闲时间点执行事件。
清除间隔函数:clearInterval
语法:clearInterval(间隔函数的ID)
异步问题案例:
Demand feedback