难题!高分请教,不够再加!

dongaotieying 2005-03-13 05:09:05
这几天我想编一个程序功能是这样的.传入一大块的网页内容.当然这里面也包含<tr>,</p>这样的字符.我想从查找到在块内容里面找到第一个出现</p>的位置.
但我调试了很长时间就是没有查找到这个位置:主要在查找"/"这个字符时候就没有了.我想这应是个特殊的字符.但网上具体的资料不多.我把这段代码粘上来.还请各位大哥多帮忙啊!
<?php
$stri="
 3月4日下午,北京现代汽车公司董事长徐和谊拨通了世界最大铁矿石公司――澳大利亚Hamersley公司中国办事处负责人的电话。当徐和谊得知进口铁矿石价格近日暴涨71.5%的<!--NEWSZW_HZH_BEGIN--> <br/>消息时,这位在执掌汽车生产之前执掌首钢的“老钢铁”表示了职业的审慎和惊异:“离谱!这样的涨幅在钢铁行业前所未见,得亲自找当事方核实”。Hamersley公司中国首代向徐和谊解释道:因美元兑欧元从0.8:1贬值到1.4:1,影响了澳大利亚、巴西等国以本币计价的铁矿石公司盈利,矿石价格不上涨将影响公司投资计划。通话后,徐缓缓放下了听筒:“这事有点闹心了。公司要迅速评估它对汽车业的影响”。<br/>2月28日,宝钢集团在其网站上公布信息称其与世界最大铁矿石生产商巴西淡水河谷公司及澳大利亚Hamersley铁矿公司经过谈判,达成2005年铁矿石价格协议:与2004年相比各类铁矿原料将分别上涨71.5%。这一涨幅与上周日本最大的钢铁企业新日铁与矿业巨头率先“锁定”的价格涨幅完全一致。日商先定了高价,根据国际铁矿石采购惯例,后签约的中方难以挣脱,十分“无奈”。
<p>  事起匆促,本报记者在周末拨通了各大汽车老总与及相关人士的电话,请他们就此对车业冲击进行推演和评估。</p>";

function cnSubStr($stri,$sublen,$num) ///要传递三个参数为第一个为:所要操作的文本内,第二个为:第二次所要截取的长度.第三个为:所要截取的次数
{
static $int_long;//整个传入的字符串内容
static $number;//计算所要截取的长度
static $str_cut;//所要返回的变量
$int_long=strlen($stri);
echo strlen($stri);
//echo $int_long;
if($num==1)
{
for($i=0;$i<$int_long;$i++)
{

if($stri{$i}=="<")
{
echo "<";
if($stri{$i+1}=="/")
{
echo "/";
if($stri{$i+2}=="p")
{
echo "p";
if($stri{$i+3}==">")
{
echo ">";
$number=i+3;
//$str_cut = substr($stri,0,i+3);//substr($stri,1,$number);
break;
}


}
}

}
}
//global $number;
//$str_cut = substr($stri,$number,$sublen);


}
if($num==1)
{
//SubStrTop($stri,$number);
//$op=0;
//$str_cut = substr($stri,0,$int_long-number);
//$str_cut = substr($stri,0,485);
$str_cut = substr($stri,0,$number);
//$str_cut = substr($stri,$number+1,$sublen);

}
elseif($num==2)
{

$str_cut = substr($stri,$number+1,$sublen);
//$str_cut = substr($stri,0,$number);

}
elseif($num==3)
{
//global $number;
//global $int_long;
$str_cut = substr($stri,$number+1+$sublen,$int_long);
}
return $str_cut;
//return $number+1;
}
$putstr=cnSubStr($stri,40,1);
echo $putstr;
?>

这就是输入:
1008<<<<
还请大家多多指教啊!
...全文
77 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
kozzi 2005-03-16
  • 打赏
  • 举报
回复
例子 1. strpos() examples

<?php
$mystring = 'abc';
$findme = 'a';
$pos = strpos($mystring, $findme);

// Note our use of ===. Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
echo "The string '$findme' was not found in the string '$mystring'";
} else {
echo "The string '$findme' was found in the string '$mystring'";
echo " and exists at position $pos";
}

// We can search for the character, ignoring anything before the offset
$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1); // $pos = 7, not 0
?>

例子 2. stripos() examples //大小写敏感

<?php
$findme = 'a';
$mystring1 = 'xyz';
$mystring2 = 'ABC';

$pos1 = stripos($mystring1, $findme);
$pos2 = stripos($mystring2, $findme);

// Nope, 'a' is certainly not in 'xyz'
if ($pos1 === false) {
echo "The string '$findme' was not found in the string '$mystring1'";
}

// Note our use of ===. Simply == would not work as expected
// because the position of 'a' is the 0th (first) character.
if ($pos2 !== false) {
echo "We found '$findme' in '$mystring2' at position $pos2";
}
?>
zalvsa 2005-03-13
  • 打赏
  • 举报
回复
呵呵,手冊上的字符處理函數庫一個可頂上面那一堆代碼。建議樓主在處理字符串時可查看手冊,幾乎可以滿足一般的開發要求。
-神仙- 2005-03-13
  • 打赏
  • 举报
回复
你就不会用strpos吗

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧