如何使用php将xml子项添加到数组的第一个索引

时间:2018-05-25 13:40:00

标签: javascript php arrays xml mathml

我有多个包含mathml(xml)元素的数组。

例如我的arraylist:

b'
<!DOCTYPE html>
<html>

<head lang="en">\n
  <title>The iterative deepening A* - Semantic Scholar</title>\n
  <meta name="robots" content="noarchive">\n \n
  <meta charset="utf-8">\n
  <meta name="s2-ui-version" content="ba8c638c99e69c67adcdd27274a81e822503dded">\n
  <meta name="description" content="An academic search engine that utilizes artificial intelligence methods to provide highly relevant results and novel tools to filter them with ease.">\n
  <meta name="twitter:description" content="An academic search engine that utilizes artificial intelligence methods to provide highly relevant results and novel tools to filter them with ease.">\n
  <meta property="og:description" content="An academic search engine that utilizes artificial intelligence methods to provide highly relevant results and novel tools to filter them with ease.">\n
  <meta property="og:title" content="The iterative deepening A* - Semantic Scholar">\n
  <meta name="twitter:title" content="The iterative deepening A* - Semantic Scholar">\n
  <meta property="og:image" content="https://www.semanticscholar.org/img/semantic_scholar_og.jpg">\n
  <meta property="og:image:secure_url" content="https://www.semanticscholar.org/img/semantic_scholar_og.jpg">\n
  <meta property="og:image:width" content="1110">\n
  <meta property="og:image:height" content="582">\n
  <meta name="twitter:image" content="https://www.semanticscholar.org/img/semantic_scholar_og.jpg">\n
  <meta property="og:type" content="website">\n
  <meta property="og:locale" content="en_US">\n
  <meta name="twitter:card" content="summary_large_image">\n
  <meta name="twitter:site" content="@allenai_org">\n
  <link rel="icon" href="/img/favicon.png" sizes="32x32">\n
  <link href="https://dab4rbh62k56j.cloudfront.net/css/main.cb86592cd7.css" rel="stylesheet">\n
  <script type="text/javascript" src="https://bam.nr-data.net/1/a59e40bc78?a=21497303&amp;sa=1&amp;v=974.7d740e1&amp;t=Unnamed%20Transaction&amp;rst=4682&amp;ref=https://www.semanticscholar.org/search&amp;be=3584&amp;fe=398&amp;dc=93&amp;af=err,xhr,ins,spa&amp;perf=%7B%22timing%22:%7B%22of%22:1527254324796,%22n%22:0,%22f%22:1428,%22dn%22:1428,%22dne%22:1428,%22c%22:1428,%22ce%22:1428,%22rq%22:1428,%22rp%22:1428,%22rpe%22:3139,%22dl%22:2811,%22di%22:3677,%22ds%22:3677,%22de%22:3677,%22dc%22:3981,%22l%22:3981,%22le%22:3983%7D,%22navigation%22:%7B%7D%7D&amp;jsonp=NREUM.setToken"></script>
  <script src="https://js-agent.newrelic.com/nr-spa-974.min.js"></script>
  <script type="text/javascript" async="" src="https://cdn.heapanalytics.com/js/heap-2424575119.js"></script>
  <script async="" src="//www.google-analytics.com/analytics.js"></script>
  <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=Promise,Array.from,Array.prototype.find,Object.values&amp;flags=gated"></script>\n
  <script>
    \
    n(function(i, s, o, g, r, a, m) {
          i[\'GoogleAnalyticsObject\']=r;i[r]=i[r]||function(){\n      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),\n      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)\n      })(window,document,\'script\',\'//www.google-analytics.com/analytics.js\',\'ga\');\n      ga(\'create\', \'UA-67668211-2\', \'auto\', { \'allowLinker\': true });\n      ga(\'require\', \'linker\');\n      ga(\'linker:autoLink\', [\'pdfs.semanticscholar.org\']);\n
  </script>\n
  <!-- Heap Analytics Snippet -->\n
  <script type="text/javascript">
    \
    n window.heap = window.heap || [], heap.load = function(e, t) {
        window.heap.appid = e, window.heap.config = t = t || {};
        var r = t.forceSSL || "https:" === document.location.protocol,
          a = document.createElement("script");
        a.type = "text/javascript", a.async = !0, a.src = (r ? "https:" : "http:") + "//cdn.heapanalytics.com/js/heap-" + e + ".js";
        var n = document.getElementsByTagName("script")[0];
        n.parentNode.insertBefore(a, n);

我希望所有数组都以Array ( [0] => <mi>x</mi><mo>=</mo><mfrac><mrow><mo>-</mo><mi>b</mi> <mo>±</mo><msqrt><msup><mi>b</mi><mn>2</mn></msup><mo>-</mo><mn>4</mn><mi>a</mi><mi>c</mi></msqrt></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac> [1] => <mi>x</mi><mo>+</mo><mn>2</mn><mo>=</mo><mn>3</mn> [2] => <mfrac><mrow><mn>2</mn><mi>x</mi><mo>+</mo><mn>2</mn></mrow><mn>3</mn></mfrac><mo>-</mo><mn>3</mn><mo>=</mo><mn>2</mn> [3] => <mo>-</mo><mn>3</mn><mo>+</mo><mn>2</mn><mo>=</mo><mi>x</mi> ) 开头,但如果它以<mo>开头,则它就像数组[3]一样好。

例如我想要的预期输出

<mo>

如您所见,所有数组都以Array ( [0] =><mo>+</mo><mi>x</mi><mo>=</mo><mfrac><mrow><mo>-</mo><mi>b</mi><mo>±</mo><msqrt><msup><mi>b</mi><mn>2</mn></msup><mo>-</mo><mn>4</mn><mi>a</mi><mi>c</mi></msqrt></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac> [1] =><mo>+</mo><mi>x</mi><mo>+</mo><mn>2</mn><mo>=</mo><mn>3</mn> [2] =><mo>+</mo><mfrac><mrow><mn>2</mn><mi>x</mi><mo>+</mo><mn>2</mn></mrow><mn>3</mn></mfrac><mo>-</mo><mn>3</mn><mo>=</mo><mn>2</mn> [3] =><mo>-</mo><mn>3</mn><mo>+</mo><mn>2</mn><mo>=</mo><mi>x</mi> ) 开头。

以下是我目前添加它的代码:

<mo>

2 个答案:

答案 0 :(得分:0)

如果你的代码存在问题,那就很多,所以我刚刚写了一些新东西......

$result = ["<mo>+</mo><mi>x</mi><mo>=</mo><mfrac><mrow><mo>-</mo><mi>b</mi><mo>±</mo><msqrt><msup><mi>b</mi><mn>2</mn></msup><mo>-</mo><mn>4</mn><mi>a</mi><mi>c</mi></msqrt></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac>",
    "<mo>+</mo><mi>x</mi><mo>+</mo><mn>2</mn><mo>=</mo><mn>3</mn>",
    "<mfrac><mrow><mn>2</mn><mi>x</mi><mo>+</mo><mn>2</mn></mrow><mn>3</mn></mfrac><mo>-</mo><mn>3</mn><mo>=</mo><mn>2</mn>",
    "<mo>-</mo><mn>3</mn><mo>+</mo><mn>2</mn><mo>=</mo><mi>x</mi>"
    ];
$arr_result=[];

for ($i=0; $i < count($result) ; $i++) {
    if (substr($result[$i],0,4)!="<mo>") {
        $arr_result[]= "<mo>+</mo>".$result[$i];
    }
    else    {
        $arr_result[]= $result[$i];
    }

}
print_r($arr_result);

这一次只是遍历每一行,检查<mo>的前4个字符,如果它不存在则将它们添加到新值中。

输出是......

Array
(
    [0] => <mo>+</mo><mi>x</mi><mo>=</mo><mfrac><mrow><mo>-</mo><mi>b</mi><mo>±</mo><msqrt><msup><mi>b</mi><mn>2</mn></msup><mo>-</mo><mn>4</mn><mi>a</mi><mi>c</mi></msqrt></mrow><mrow><mn>2</mn><mi>a</mi></mrow></mfrac>
    [1] => <mo>+</mo><mi>x</mi><mo>+</mo><mn>2</mn><mo>=</mo><mn>3</mn>
    [2] => <mo>+</mo><mfrac><mrow><mn>2</mn><mi>x</mi><mo>+</mo><mn>2</mn></mrow><mn>3</mn></mfrac><mo>-</mo><mn>3</mn><mo>=</mo><mn>2</mn>
    [3] => <mo>-</mo><mn>3</mn><mo>+</mo><mn>2</mn><mo>=</mo><mi>x</mi>
)

答案 1 :(得分:0)

您可以尝试在for循环中执行类似的操作。 strpos将返回您正在搜索的字符串中存在的字符串的位置。因此,在这种情况下,如果字符串以“”开头,则返回0。

$your_string = "<mi>x</mi><mo>+</mo><mn>2</mn><mo>=</mo><mn>3</mn>";
$searching_tag = "<mo>";

$pos = strpos($your_string, $searching_tag);

if($pos === 0)
{
   //string starts with "<mo>"
}
else
{
   //string doesn't start with "<mo>"
}