我的问题很简单,我有一个类似下面的字符串,这是非常不规则的:
package:name ='com.adobe.reader'versionCode ='77969' versionName = '10 .6.1'application-label:'Adobe Reader' application-label-da:'Adobe Reader'application-label-ja:'Adobe 读者'application-label-de:'Adobe Reader' application-label-nl:'Adobe Reader'application-label-pl:'Adobe Reader'application-label-en:'Adobe Reader' application-label-ko:'Adobe Reader'应用程序标签-fr:'Adobe 读者'application-label-tr:'Adobe Reader' application-label-cs:'Adobe Reader'application-label-es:'Adobe 读者'应用标签 - 它''Adobe Reader' application-label-pt:'Adobe Reader'application-label-ru:'Adobe 读者'application-label-sv:'Adobe Reader' application-label-zh_CN:'Adobe Reader'application-label-zh_TW:'Adobe 读者' 应用程序图标-160: 'RES /抽拉-MDPI / reader_app_android.png' 应用程序图标-240: 'RES /抽拉-HDPI / reader_app_android.png' 应用程序图标-320: 'RES /抽拉-xhdpi / reader_app_android.png' 应用程序:label ='Adobe Reader' icon ='res / drawable-mdpi / reader_app_android.png'可启动活动: name ='com.adobe.reader.AdobeReader'label =''icon =''sdkVersion:'8' 使用许可权:“com.adobe.reader.provider.permission.READ” 使用许可权:“android.permission.INTERNET对” 使用许可权:“android.permission.WRITE_EXTERNAL_STORAGE” 使用许可权:“android.permission.ACCESS_NETWORK_STATE” 使用许可权:“android.permission.READ_EXTERNAL_STORAGE” 使用-隐含权限: 'android.permission.READ_EXTERNAL_STORAGE','请求 WRITE_EXTERNAL_STORAGE'uses-feature:'android.hardware.touchscreen' uses-implied-feature:'android.hardware.touchscreen','假设你 除非明确选择'main',否则需要触摸屏 其他活动其他服务支持屏幕:'小''正常' 'large''xlarge'支持 - 任何密度:'true'语言环境:'--_--''da' 'j''de''nl''pl''en''ko''fr''tr''cs''es''it''pt''ru''sv' 'zh_CN''zh_TW'密度:'160''240''320'原生代码:'armeabi' 'armeabi-V7A'
是否可以在PHP中解析此字符串中的某些值(例如versionCode
的值)?
答案 0 :(得分:1)
您可以使用preg_match。
例如:
<?php
$str = '...';
function parseStr($key, $str)
{
$regex = '/(?:'.$key.'[=:]{1})\s?\'?([^\'\s]+)\'?\s/si';
$str = rtrim($str).' ';
return preg_match($regex, $str, $matches) ? $matches[1] : false;
}
var_dump(parseStr('versionCode', $str)); // Use var_dump for debugging/testing,
// put the output in a var otherwise.
以下是它的作用:
Regex '/(?:'.$key.'\=)\'?([^\'\s]+)\'?\s/si':
(?: => excludes the key from the match
'.$key.'[=:]{1}) => finds the key in the string
[=:]{1} => either = or : comes after the key
\s?\'? => a space or ' is optional at this point
([^\'\s]+) => matches the desired value
\'?\s => again, ' is optional but a space is required
/si => s = single-line mode and i = case insensitive
rtrim($str) strips the last space (if any) of the string and .' ' adds it again.
This is done, because of the regex "\'?\s" part.
return $pr ? $match[1] : false; means, return the value or false if no match.
试一试。
答案 1 :(得分:1)
如果您想在PHP中执行此操作,可以执行以下操作:
function find_var($key, $str) {
$temp = substr($str, (strpos($str, $key)+(strlen($key)+1)));
return substr($temp, 0, strpos($temp, "'"));
}
$val = "package: name='com.adobe.reader' versionCode='77969' versionName='10.6.1' application-label:'Adobe Reader' application-label-da:'Adobe Reader' application-label-ja:'Adobe Reader' application-label-de:'Adobe Reader' application-label-nl:'Adobe Reader' application-label-pl:'Adobe Reader' application-label-en:'Adobe Reader' application-label-ko:'Adobe Reader' application-label-fr:'Adobe Reader' application-label-tr:'Adobe Reader' application-label-cs:'Adobe Reader' application-label-es:'Adobe Reader' application-label-it:'Adobe Reader' application-label-pt:'Adobe Reader' application-label-ru:'Adobe Reader' application-label-sv:'Adobe Reader' application-label-zh_CN:'Adobe Reader' application-label-zh_TW:'Adobe Reader' application-icon-160:'res/drawable-mdpi/reader_app_android.png' application-icon-240:'res/drawable-hdpi/reader_app_android.png' application-icon-320:'res/drawable-xhdpi/reader_app_android.png' application: label='Adobe Reader' icon='res/drawable-mdpi/reader_app_android.png' launchable-activity: name='com.adobe.reader.AdobeReader' label='' icon='' sdkVersion:'8' uses-permission:'com.adobe.reader.provider.permission.READ' uses-permission:'android.permission.INTERNET' uses-permission:'android.permission.WRITE_EXTERNAL_STORAGE' uses-permission:'android.permission.ACCESS_NETWORK_STATE' uses-permission:'android.permission.READ_EXTERNAL_STORAGE' uses-implied-permission:'android.permission.READ_EXTERNAL_STORAGE','requested WRITE_EXTERNAL_STORAGE' uses-feature:'android.hardware.touchscreen' uses-implied-feature:'android.hardware.touchscreen','assumed you require a touch screen unless explicitly made optional' main other-activities other-services supports-screens: 'small' 'normal' 'large' 'xlarge' supports-any-density: 'true' locales: '--_--' 'da' 'ja' 'de' 'nl' 'pl' 'en' 'ko' 'fr' 'tr' 'cs' 'es' 'it' 'pt' 'ru' 'sv' 'zh_CN' 'zh_TW' densities: '160' '240' '320' native-code: 'armeabi' 'armeabi-v7a'";
echo find_var('versionCode=', $val);
以上输出: 77969
echo find_var('application-label:', $val);
以上输出: Adobe Reader
试一试:Run PHP Online
用伪解释函数:
- 在位置 $ key 加上1个字符(')后,在传递的字符串中创建一个字符串。
- 返回从临时字符串开头到第一次出现'
的字符串