首页 > 编程技术 > php

php 正则替换函数 ereg_replace

发布时间:2016-11-25 16:50

下面的实例是利用php 正则替换函数 ereg_replace来把指定的字符替换成我想需要的字符实例。

    

 代码如下 复制代码
$num = 'www.111cn.net';
     $string = "this string has four words. <br>";
     $string = ereg_replace ('four', $num, $string);
     echo $string;
 
     $num = '49';
     $string = "this string has four words";
     $string = ereg_replace ('four', $num, $string);
     echo $string;
 
 
   $string ="测试用文字";
   echo "**********$string**********<p>";
   $string = ereg_replace ("^", "<br>", $string);
   $string = ereg_replace ("$", "<br>", $string);
   echo "==========$string==========";

var_export
(php教程 4 >= 4.2.0, php 5)
var_export -- 输出或返回一个变量的字符串表示
描述

mixed var_export ( mixed expression [, bool return] )

此函数返回关于传递给该函数的变量的结构信息,它和 var_dump() 类似,不同的是其返回的表示是合法的 php 代码。
您可以通过将函数的第二个参数设置为 true,从而返回变量的表示。
eg:

var_export(array('a','b',array('aa','bb','cc'))) 这种与var_dump没什么区别;

$var =var_export(array('a','b',array('aa','bb','cc')),true),加上true后,不会再打印出来,而是给了一个变量,这样就可以直接输出;
echo $var;此时输出来的形式与var_dump()打印的相似。
eg2 
代码如下:

$data = array ('name' => 'abc', 'job' => 'programmer','a'=>array('aa','cc','bb'));
$data = var_export($data,true);
echo $data;

输出形式如下: 
代码如下:

array (
'name' => 'abc',
'job' => 'programmer',
'a' =>
array (
0 => 'aa',
1 => 'cc',
2 => 'bb',
),
)
本教程是一款php ajax返回 json数据实例哦,就是利用ajax实时的接受json.php文件发送的数据请求,并且进行了处理。
 代码如下 复制代码

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>php教程 ajax返回 网页特效on数据实例</title>
<script type="text/网页特效" language="javascript">
var xmlhttp;
function createxmlhttprequest()
{
//var xmlhttp=null;
try
  {
  // firefox, opera 8.0+, safari
  xmlhttp=new xmlhttprequest();
  }
catch (e)
  {
  // internet explorer
  try
    {
    xmlhttp=new activexobject("msxml2.xmlhttp");
    }
  catch (e)
    {
    xmlhttp=new activexobject("microsoft.xmlhttp");
    }
  }
return xmlhttp;
}
function startrequest(id)
{
    createxmlhttprequest();
    try
    {  
     url="json.php?cid="+id;
        xmlhttp.onreadystatechange = handlestatechange;
        xmlhttp.open("post", url, true);
        xmlhttp.send(null);
    }
    catch(exception)
    {
        alert("xmlhttp fail");
    }
}
function handlestatechange()
{
    if(xmlhttp.readystate == 4)
    {
        if (xmlhttp.status == 200 || xmlhttp.status == 0)
        {
            var result = xmlhttp.responsetext;
            var json = eval("(" + result + ")");
            alert('name:'+json.name);
            alert('age:'+json.age);
   alert('id:'+json.id);
        }
    }
}
</script>
</head>

<body>
<div>
        <input type="button" value="ajaxtest" onclick="startrequest(5);" />
    </div>
</body>
</html>

json.php 文件

 代码如下 复制代码

<?php
/**************************************************************
 *
 * 使用特定function对数组中所有元素做处理
 * @param string &$array  要处理的字符串
 * @param string $function 要执行的函数
 * @return boolean $apply_to_keys_also  是否也应用到key上
 * @access public
 *
 *************************************************************/
function arrayrecursive(&$array, $function, $apply_to_keys_also = false)
{
    static $recursive_counter = 0;
    if (++$recursive_counter > 1000) {
        die('possible deep recursion attack');
    }
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            arrayrecursive($array[$key], $function, $apply_to_keys_also);
        } else {
            $array[$key] = $function($value);
        }

        if ($apply_to_keys_also && is_string($key)) {
            $new_key = $function($key);
            if ($new_key != $key) {
                $array[$new_key] = $array[$key];
                unset($array[$key]);
            }
        }
    }
    $recursive_counter--;
}

/**************************************************************
 *
 * 将数组转换为json字符串(兼容中文)
 * @param array $array  要转换的数组
 * @return string  转换得到的json字符串
 * @access public
 *
 *************************************************************/
function json($array) {
 arrayrecursive($array, 'urlencode', true);
 $json = json_encode($array);
 return urldecode($json);
}

$array = array
       (
          'name'=>'希亚',
          'age'=>20,
    'id'=>$_post['cid']
       );


 

echo json($array);
/*********
 {"name":"希亚","age":"20"}
?>

这是一款简单的php目录与文件操作的入门教程,我们主要是讲三个函数opendir、readdir和closedir的使用方法。
 代码如下 复制代码

$dir = "d:www.111cn.net";

//打开目录$dir,并将目录句柄赋给变量$dh
if($dh = opendir($dir))
{
    //通过while循环,使用函数readdir获取文件名
    while(($file_name = readdir($dh)) !== false)
    {
        echo "file name: ".$file_name;
        echo "<br/>";
        echo "<br/>";
    }
   
    //处理完成后,关闭目录句柄$dh
    closedir($dh);
}

/*
opendir定义和用法
opendir() 函数打开一个目录句柄,可由 closedir(),readdir() 和 rewinddir() 使用。

若成功,则该函数返回一个目录流,否则返回 false 以及一个 error。可以通过在函数名前加上 "@" 来隐藏 error 的输出。

语法
opendir(path,context)


readdir定义和用法
readdir() 函数返回由 opendir() 打开的目录句柄中的条目。

若成功,则该函数返回一个文件名,否则返回 false。

语法
readdir(dir_stream)

closedir() 函数关闭由 opendir() 函数打开的目录句柄。

语法
closedir(dir_stream)

php 读取文件内容与向文件写入数据这里主要是讲一行行写数据到文件,也一行行读出文件的内容。

*/

 代码如下 复制代码

$fp = fopen($_server['document_root']."/../data/info.dat",'r');

if(!$fp)
{
    echo "<b>error: 打开文件错误,请检查目录是否正确,或稍后再试!</b>";
    exit;
}

while(!feof($fp))
{
    $line = fgets($fp);
    echo $line;
    echo '<br/>';
}

fclose($fp);

//写文件

 代码如下 复制代码

$file = "data.txt";
$content = "内容标题 www.111cn.net 内容第二行";    //要写入的内容

if(!$fp = fopen($file,'a'))                           //打开文件$file时,使用追加模式,此时文件指针会在文件开始处
{
    echo "打开文件$file失败!";
    exit;
}

if(fwrite($fp,$content) === false)                    //将内容写入文件
{
    echo "写入文件失败!";
    exit;
}

echo "写入文件成功!";
fclose($fp);

 

 

标签:[!--infotagslink--]

您可能感兴趣的文章: