찾다
백엔드 개발PHP 튜토리얼在线等,急。PHP替换指定位置的字符串


$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';
$b='38';
$c='85';

用PHP怎么做一替换。。。 

就是说 当$a中 包含$b中的字数后。 把38,0.00,0.00,100 中的100替换成$c中的数字。

如果$b=‘37’

那么就把37,0.00,0.00,100 中的100替换成$c中的数字。


谢谢各位了


回复讨论(解决方案)

php5.3+ 通过

$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85';echo preg_replace_callback("/(^|\|$b,0.00,0.00,)100/", function($m) use ($c){ return $m[1].$c;}, $a);
36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,85|39,0.00,0.00,100

php5.5- 还可写作

echo preg_replace("/(^|\|$b,0.00,0.00,)100/e", "'$1'.'$c'", $a);

用新的子字符串替换原始字符串中被指定要替换的字符串
$str2="某某";
$str1="**";
$str="   某某公司是一家以计算机软件技术为核心的高科技企业,多年来始终致力于行业管理软件开发、数字化出版物制作、计算机网络系统综合应用以及行业电子商务网站开发等领域,涉及生产、管理、控制、仓贮、物流、营销、服务等行业";
echo str_ireplace($str2,$str1,$str);
?>

php5.3+ 通过

$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85';echo preg_replace_callback("/(^|\|$b,0.00,0.00,)100/", function($m) use ($c){ return $m[1].$c;}, $a);
36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,85|39,0.00,0.00,100



老大。如果兼容PHP5.0的话  要怎么写呢。

#2 不就是吗?

#2 不就是吗?



哥哥 在帮我一下
$a = '36, 0.00, 0.00, 100|37, 0.00, 0.00, 100|38, 0.00, 0.00, 100|39, 0.00, 0.00, 100';

红色部份的值不是固定的。。那这个正规要怎么弄呀。。。。。。。谢谢了

不固定?
那你描述的不是固定的吗?

不固定?
那你描述的不是固定的吗?


应该不固定吧, 如果固定,还要他有用吗?

$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.01,0.00,100|39,0.00,0.00,100';$b='38';$c='85';echo "{$a}<br>";echo preg_replace_callback("/(^|\|$b,0.00,0.00,)100/", function($m) use ($c){ return $m[1].$c;}, $a).'<br>';echo preg_replace_callback("/(^|\|$b,\d\.\d{1,2},\d\.\d{1,2},)100/", function($m) use ($c){ return $m[1].$c;}, $a);


\d\.\d{1,2} 这个确定 中间 的0.00, 别的格式自行调整

好吧,。,。是我错了。。。。我没有表答清楚。我开始以为很简单。结果我自已试了一下发现还不行

实际我要的效果是这样的

$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';
$b='38';
$c='85';

就是说 当$a中 包含$b中的字数后。 把38,0.00,0.00,100 中的100取出来   然后用这个100 减去$c里面的数字。
最后等于这样 36,0.00,0.00,100|37,0.00,0.00,100| 38,0.00,0.00,15|39,0.00,0.00,100

然后入库、。。。。。。。其它我想要这个。。。把问题想简单了

求各位帮个忙吧

$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85'; echo preg_replace_callback("/(^|\|$b,0.00,0.00,)(100)/", function($m) use ($c){ return $m[1].($m[2]-$c);}, $a);
36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,15|39,0.00,0.00,100

噢,你的 php 版本没那么高
$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85'; echo preg_replace("/(^|\|$b,0.00,0.00,)(100)/e", "'$1'.($2-$c)", $a);
36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,15|39,0.00,0.00,100

如果都是变数
$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85'; echo preg_replace("/((?:^|\|$b),(?:[\d.]+,){2})(\d+)/e", "'$1'.($2-$c)", $a);
36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,15|39,0.00,0.00,100

$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85'; echo preg_replace_callback("/(^|\|$b,0.00,0.00,)(100)/", function($m) use ($c){ return $m[1].($m[2]-$c);}, $a);
36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,15|39,0.00,0.00,100

噢,你的 php 版本没那么高
$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85'; echo preg_replace("/(^|\|$b,0.00,0.00,)(100)/e", "'$1'.($2-$c)", $a);
36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,15|39,0.00,0.00,100

如果都是变数
$a = '36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,100|39,0.00,0.00,100';$b='38';$c='85'; echo preg_replace("/((?:^|\|$b),(?:[\d.]+,){2})(\d+)/e", "'$1'.($2-$c)", $a);
36,0.00,0.00,100|37,0.00,0.00,100|38,0.00,0.00,15|39,0.00,0.00,100



啥也不说了。眼泪哇哇地。。。太感谢您了。。。@xuzuning 
성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
11 최고의 PHP URL 쇼트너 스크립트 (무료 및 프리미엄)11 최고의 PHP URL 쇼트너 스크립트 (무료 및 프리미엄)Mar 03, 2025 am 10:49 AM

종종 키워드와 추적 매개 변수로 혼란스러워하는 긴 URL은 방문자를 방해 할 수 있습니다. URL 단축 스크립트는 솔루션을 제공하여 소셜 미디어 및 기타 플랫폼에 이상적인 간결한 링크를 만듭니다. 이 스크립트는 개별 웹 사이트 a에 유용합니다

Laravel의 플래시 세션 데이터로 작업합니다Laravel의 플래시 세션 데이터로 작업합니다Mar 12, 2025 pm 05:08 PM

Laravel은 직관적 인 플래시 방법을 사용하여 임시 세션 데이터 처리를 단순화합니다. 응용 프로그램에 간단한 메시지, 경고 또는 알림을 표시하는 데 적합합니다. 데이터는 기본적으로 후속 요청에만 지속됩니다. $ 요청-

Laravel Back End : Part 2, React가있는 React 앱 구축Laravel Back End : Part 2, React가있는 React 앱 구축Mar 04, 2025 am 09:33 AM

이것은 Laravel 백엔드가있는 React Application을 구축하는 데있어 시리즈의 두 번째이자 마지막 부분입니다. 이 시리즈의 첫 번째 부분에서는 기본 제품 목록 응용 프로그램을 위해 Laravel을 사용하여 편안한 API를 만들었습니다. 이 튜토리얼에서는 Dev가 될 것입니다

Laravel 테스트에서 단순화 된 HTTP 응답 조롱Laravel 테스트에서 단순화 된 HTTP 응답 조롱Mar 12, 2025 pm 05:09 PM

Laravel은 간결한 HTTP 응답 시뮬레이션 구문을 제공하여 HTTP 상호 작용 테스트를 단순화합니다. 이 접근법은 테스트 시뮬레이션을보다 직관적으로 만들면서 코드 중복성을 크게 줄입니다. 기본 구현은 다양한 응답 유형 단축키를 제공합니다. Illuminate \ support \ Facades \ http를 사용하십시오. http :: 가짜 ([ 'google.com'=> ​​'Hello World', 'github.com'=> ​​[ 'foo'=> 'bar'], 'forge.laravel.com'=>

PHP의 컬 : REST API에서 PHP Curl Extension 사용 방법PHP의 컬 : REST API에서 PHP Curl Extension 사용 방법Mar 14, 2025 am 11:42 AM

PHP 클라이언트 URL (CURL) 확장자는 개발자를위한 강력한 도구이며 원격 서버 및 REST API와의 원활한 상호 작용을 가능하게합니다. PHP CURL은 존경받는 다중 프로모토콜 파일 전송 라이브러리 인 Libcurl을 활용하여 효율적인 execu를 용이하게합니다.

Codecanyon에서 12 개의 최고의 PHP 채팅 스크립트Codecanyon에서 12 개의 최고의 PHP 채팅 스크립트Mar 13, 2025 pm 12:08 PM

고객의 가장 긴급한 문제에 실시간 인스턴트 솔루션을 제공하고 싶습니까? 라이브 채팅을 통해 고객과 실시간 대화를 나누고 문제를 즉시 해결할 수 있습니다. 그것은 당신이 당신의 관습에 더 빠른 서비스를 제공 할 수 있도록합니다.

2025 PHP 상황 조사 발표2025 PHP 상황 조사 발표Mar 03, 2025 pm 04:20 PM

2025 PHP Landscape Survey는 현재 PHP 개발 동향을 조사합니다. 개발자와 비즈니스에 대한 통찰력을 제공하는 프레임 워크 사용, 배포 방법 및 과제를 탐색합니다. 이 조사는 현대 PHP Versio의 성장을 예상합니다

라 라벨에서 알림라 라벨에서 알림Mar 04, 2025 am 09:22 AM

이 기사에서는 Laravel 웹 프레임 워크에서 알림 시스템을 탐색 할 것입니다. Laravel의 알림 시스템을 사용하면 다른 채널을 통해 사용자에게 알림을 보낼 수 있습니다. 오늘은 알림을 보낼 수있는 방법에 대해 논의합니다

See all articles

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

Dreamweaver Mac版

Dreamweaver Mac版

시각적 웹 개발 도구

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

SublimeText3 영어 버전

SublimeText3 영어 버전

권장 사항: Win 버전, 코드 프롬프트 지원!

DVWA

DVWA

DVWA(Damn Vulnerable Web App)는 매우 취약한 PHP/MySQL 웹 애플리케이션입니다. 주요 목표는 보안 전문가가 법적 환경에서 자신의 기술과 도구를 테스트하고, 웹 개발자가 웹 응용 프로그램 보안 프로세스를 더 잘 이해할 수 있도록 돕고, 교사/학생이 교실 환경 웹 응용 프로그램에서 가르치고 배울 수 있도록 돕는 것입니다. 보안. DVWA의 목표는 다양한 난이도의 간단하고 간단한 인터페이스를 통해 가장 일반적인 웹 취약점 중 일부를 연습하는 것입니다. 이 소프트웨어는