0

我的问题很简单,我有一个像下面这样非常不规则的字符串:

包: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' 应用程序:标签='Adobe Reader' 图标='res/drawable-mdpi/reader_app_android.png' 启动活动:名称='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' 使用权限:'android.permission.ACCESS_NETWORK_STATE' 使用权限:'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','假设你需要一个触摸屏除非明确设为可选'主要其他活动其他服务支持屏幕:'小''正常''大''xlarge'支持-任何密度:'true'语言环境:'--_--''da'' ja''de''nl''pl''en''ko''fr''tr''cs''es''it''pt''ru''sv''zh_CN''zh_TW' 密度:' 160''240''320'本机代码:'armeabi''armeabi-v7a'android.permission.READ_EXTERNAL_STORAGE','requested WRITE_EXTERNAL_STORAGE' uses-feature:'android.hardware.touchscreen' uses-implied-feature:'android.hardware.touchscreen','假设您需要触摸屏,除非明确设为可选'主要其他-activities 其他服务支持-屏幕:'small' 'normal' 'large' 'xlarge' 支持-任何密度:'true' 区域设置:'--_--' 'da' 'ja' 'de' 'nl ''pl''en''ko''fr''tr''cs''es''it''pt''ru''sv''zh_CN''zh_TW' 密度:'160''240''320 ' 本机代码:'armeabi' 'armeabi-v7a'android.permission.READ_EXTERNAL_STORAGE','requested WRITE_EXTERNAL_STORAGE' uses-feature:'android.hardware.touchscreen' uses-implied-feature:'android.hardware.touchscreen','假设您需要触摸屏,除非明确设为可选'主要其他-activities 其他服务支持-屏幕:'small' 'normal' 'large' 'xlarge' 支持-任何密度:'true' 区域设置:'--_--' 'da' 'ja' 'de' 'nl ''pl''en''ko''fr''tr''cs''es''it''pt''ru''sv''zh_CN''zh_TW' 密度:'160''240''320 ' 本机代码:'armeabi' 'armeabi-v7a'请求的 WRITE_EXTERNAL_STORAGE' uses-feature:'android.hardware.touchscreen' uses-implied-feature:'android.hardware.touchscreen','假设您需要触摸屏,除非明确设为可选'主要其他活动其他服务支持屏幕: 'small' 'normal' 'large' 'xlarge' 支持任何密度:'true' 语言环境:'--_--' 'da' 'ja' 'de' 'nl' 'pl' 'en' ' ko' 'fr' 'tr' 'cs' 'es' 'it' 'pt' 'ru' 'sv' 'zh_CN' 'zh_TW' 密度:'160' '240' '320' 本机代码:'armeabi' 'armeabi-v7a'请求的 WRITE_EXTERNAL_STORAGE' uses-feature:'android.hardware.touchscreen' uses-implied-feature:'android.hardware.touchscreen','假设您需要触摸屏,除非明确设为可选'主要其他活动其他服务支持屏幕: 'small' 'normal' 'large' 'xlarge' 支持任何密度:'true' 语言环境:'--_--' 'da' 'ja' 'de' 'nl' 'pl' 'en' ' ko' 'fr' 'tr' 'cs' 'es' 'it' 'pt' 'ru' 'sv' 'zh_CN' 'zh_TW' 密度:'160' '240' '320' 本机代码:'armeabi' 'armeabi-v7a'uses-implied-feature:'android.hardware.touchscreen','假设你需要一个触摸屏,除非明确设为可选'主要其他活动其他服务支持-屏幕:'小''正常''大''xlarge'支持-任何密度:'true'语言环境:'--_--''da''ja''de''nl''pl''en''ko''fr''tr''cs''es' 'it' 'pt' 'ru' 'sv' 'zh_CN' 'zh_TW' 密度:'160' '240' '320' 本机代码:'armeabi' 'armeabi-v7a'uses-implied-feature:'android.hardware.touchscreen','假设你需要一个触摸屏,除非明确设为可选'主要其他活动其他服务支持-屏幕:'小''正常''大''xlarge'支持-任何密度:'true'语言环境:'--_--''da''ja''de''nl''pl''en''ko''fr''tr''cs''es' 'it' 'pt' 'ru' 'sv' 'zh_CN' 'zh_TW' 密度:'160' '240' '320' 本机代码:'armeabi' 'armeabi-v7a'small' 'normal' 'large' 'xlarge' 支持任何密度:'true' 语言环境:'--_--' 'da' 'ja' 'de' 'nl' 'pl' 'en' 'ko' 'fr' 'tr' 'cs' 'es' 'it' 'pt' 'ru' 'sv' 'zh_CN' 'zh_TW' 密度:'160' '240' '320' 本机代码:'armeabi' 'armeabi -v7a'small' 'normal' 'large' 'xlarge' 支持任何密度:'true' 语言环境:'--_--' 'da' 'ja' 'de' 'nl' 'pl' 'en' 'ko' 'fr' 'tr' 'cs' 'es' 'it' 'pt' 'ru' 'sv' 'zh_CN' 'zh_TW' 密度:'160' '240' '320' 本机代码:'armeabi' 'armeabi -v7a''320' 本机代码:'armeabi' 'armeabi-v7a''320' 本机代码:'armeabi' 'armeabi-v7a'

versionCode是否可以在 PHP中解析这个字符串中的一些值(例如 的值)?

4

2 回答 2

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.

试试看。

于 2013-08-01T21:56:12.617 回答
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

试一试:在线运行 PHP

用伪解释函数:

  • 在传递的字符串中找到位置$key加上 1 个字符 (')后创建一个字符串。
  • 返回从临时字符串的开头到第一次出现'的字符串
于 2013-08-01T22:20:46.917 回答