English 中文(简体)
字体字面上的错误值?
原标题:Error when get value from string text?
  • 时间:2012-04-17 10:15:48
  •  标签:
  • php

我的案文是:

$text =  abc def abc ghi abc jkl ;
$search =  abc ;
$regex =  / .trim($search). /ism ;
if(preg_match_all($regex, $text, $tmp)) {
   $array_key = $tmp[0];
   foreach($array_key as $ak) {
       echo $ak[1];
   }
}

如果是b,我希望结果是abc,如何确定。

abc def abc ghi abc jkl

最佳回答
if(preg_match_all($regex, $text, $tmp)) {
   $array_key = $tmp[0];
   echo $array_key[1];
}
问题回答

<代码> 在座椅上?

Well,$tmp,在您的名册上将拥有一系列的所有领域。 然后,这些阵列将包含这些领域的所有价值观。

您的代码是:$ak = $[0][$x] = “abc”,在您通过纳克/代码>时,您的职等比实际阵列高出1,并得abc” = “b”

我想你希望<代码>echo$ak;

I am trying to discuss within your code. I hope its will help

<?php
$text =  abc def abc ghi abc jkl ;
$search =  abc ;
$regex =  / .trim($search). /ism ;
if(preg_match_all($regex, $text, $tmp)) 
{
  $array_key = $tmp[0];

  /*Test using print_r($tmp); 
    then you will find result: Array( [0] => Array ( [0] => abc [1] => abc [2] =>      
     abc) ) 
   If you want to test/print first array value then echo $array_key[0]; result is: abc
   Similarly for $array_key[1] or $array_key[2] result is abc
   That means your array contains 3 same value
   */

   foreach($array_key as $ak) 
   {
    /*If you use echo $ak[1]; then it will print b because in foreach loop $ak value   
    is:  abc 
    You can t print only abc in foreach loop. To print abc you don t need to use   
    foreach loop. Just use echo $array_key[0]; 
   */ 
   }
  }
  ?>

  Maybe your expected code is:
  <?php
    $text =  abc def abc ghi abc jkl ;
    $search =  abc ;
    $regex =  / .trim($search). /ism ;
    if(preg_match_all($regex, $text, $tmp)) 
    {
     $array_key = $tmp[0];
     echo $array_key[0];
    }
   ?>
  Output: abc




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签