Solved

Str_replace php not working as expected

Posted on 2016-09-15
5
49 Views
Last Modified: 2016-09-15
Hi I have this situation where I need to replace a string within a string of text:


$string = <div style=\"margin-bottom:10px;\"><label class=\"description\" for=\"conviction\">Conviction </label><br /><select id=\"conviction_0\" name=\"convictionDetails[0][conviction]\"><option value=\"AC10\">AC10 FAILING TO STOP AFTER AN ACCIDENT </option><option value=\"AC12\">AC12 AIDING AND ABETTING (RELATING TO AC10) </option><option value=\"AC14\">AC14 CAUSING OR PERMITTING (RELATING TO AC10) </option><option value=\"AC20\">AC20 FAILING TO GIVE PARTICULARS OR REPORT IN 24HR </option><option value=\"AC30\">AC30 UNDEFINED ACCIDENT OFFENCES </option><option value=\"BA10\">BA10 DRIVING WHILE DISQUALIFIED BY ORDER OF COURT </option><option value=\"BA20\">BA20 DRIVING WHILE DISQUALIFIED AS UNDER AGE </option><option value=\"BA30\">BA30 ATTEMPTING TO DRIVE WHILE DISQUALIFIED </option><option value=\"CD10\">CD10 DRIVING WITHOUT DUE CARE AND ATTENTION </option><option value=\"CD20\">CD20 DRIVING WITHOUT REASONABLE CONSIDERATION </option><option value=\"CD30\">CD30 DRIVING WITHOUT DUE CARE & ATTENTION </option><option value=\"CD40\">CD40 CAUSING DEATH - DRIVING WHEN UNFIT - DRINK </option><option value=\"CD50\">CD50 CAUSING DEATH - DRIVING WHEN UNFIT - DRUGS </option><option value=\"CD60\">CD60 CAUSING DEATH - DRIVING OVER ALCOHOL LIMIT </option><option value=\"CD70\">CD70 CAUSING DEATH - DRIVING & SPECIMEN REFUSED </option><option value=\"CU10\">CU10 DEFECTIVE BRAKES </option><option value=\"CU20\">CU20 DANGEROUS USE (NOT BRAKES, STEERING OR TYRES) </option><option value=\"CU30\">CU30 DEFECTIVE TYRES </option><option value=\"CU40\">CU40 DEFECTIVE STEERING </option><option value=\"CU50\">CU50 DANGEROUS LOAD </option><option value=\"CU60\">CU60 UNDEFINED CONSTRUCTION AND USE FAILURE </option><option value=\"CU80\">CU80 DRIVING WHILST USING A MOBILE PHONE </option><option value=\"DD10\">DD10 DANGEROUS DRIVING </option><option value=\"DD20\">DD20 DANGEROUS SPEED </option><option value=\"DD30\">DD30 RECKLESS DRIVING </option><option value=\"DD40\">DD40 COMBINATION OF DANGEROUS, SPEED OR RECKLESS </option><option value=\"DD50\">DD50 DEATH BY DANGEROUS DRIVING </option><option value=\"DD60\">DD60 CULPABLE HOMICIDE WHILE DRIVING </option><option value=\"DD70\">DD70 DEATH BY RECKLESS DRIVING </option><option value=\"DD80\">DD80 CAUSING DEATH BY DANGEROUS DRIVING </option><option value=\"DR10\">DR10 DRIVING WITH ALCOHOL LEVEL ABOVE LIMIT </option><option value=\"DR20\">DR20 DRIVING WHILE UNFIT THROUGH DRINK </option><option value=\"DR30\">DR30 DRIVING THEN FAILING TO SUPPLY A SPECIMEN </option><option value=\"DR40\">DR40 IN CHARGE OF A VEHICLE - ALCOHOL ABOVE LIMIT </option><option value=\"DR50\">DR50 IN CHARGE OF A VEHICLE - UNFIT THROUGH DRINK </option><option value=\"DR60\">DR60 FAILURE - PROVIDE SPECIMEN OTHER THAN DRIVING </option><option value=\"DR70\">DR70 FAILING TO PROVIDE SPECIMEN FOR BREATH TEST </option><option value=\"DR80\">DR80 DRIVING WHEN UNFIT THROUGH DRUGS </option><option value=\"DR90\">DR90 IN CHARGE OF VEHICLE WHEN UNFIT THROUGH DRUGS </option><option value=\"IN10\">IN10 DRIVING UNINSURED </option><option value=\"IN12\">IN12 AIDING AND ABETTING (RELATING TO IN10) </option><option value=\"IN14\">IN14 CAUSING OR PERMITTING (RELATING TO IN10) </option><option value=\"LC10\">LC10 DRIVING WITHOUT A LICENCE </option><option value=\"LC20\">LC20 DRIVING NOT IN ACCORDANCE WITH A LICENCE </option><option value=\"LC30\">LC30 DRIVING AFTER MAKING A FALSE DECLARATION </option><option value=\"LC40\">LC40 DRIVING FAILED TO NOTIFY OF A DISABILITY </option><option value=\"LC50\">LC50 DRIVING AFTER LICENCE REVOKED MEDICAL GROUNDS </option><option value=\"MS10\">MS10 LEAVING A VEHICLE IN A DANGEROUS POSITION </option><option value=\"MS20\">MS20 UNLAWFUL PILLION RIDING </option><option value=\"MS30\">MS30 PLAYSTREET OFFENCES </option><option value=\"MS40\">MS40 DRIVING - UNCORRECTED EYESIGHT/REFUSING TEST </option><option value=\"MS50\">MS50 MOTOR RACING ON THE HIGHWAY </option><option value=\"MS60\">MS60 OFFENCES NOT COVERED BY OTHER CODES </option><option value=\"MS70\">MS70 DRIVING WITH UNCORRECTED DEFECTIVE EYESIGHT </option><option value=\"MS80\">MS80 REFUSING TO SUBMIT TO AN EYESIGHT TEST </option><option value=\"MS90\">MS90 FAILURE TO GIVE INFORMATION AS TO DRIVER ID </option><option value=\"MT01\">MT01 DRIVING WHILST USING A MOBILE TELEPHONE </option><option value=\"MW10\">MW10 CONTRAVENING SPECIAL ROAD REGULATIONS </option><option value=\"NE99\">NE99 NON ENDORSABLE OFFENCE </option><option value=\"PC10\">PC10 PEDESTRIAN CROSSING (UNDEFINED) </option><option value=\"PC20\">PC20 MOVING VEHICLE AND PEDESTRIAN CROSSING </option><option value=\"PC30\">PC30 STATIONARY VEHICLE AND PEDESTRIAN CROSSING </option><option value=\"PL10\">PL10 DRIVING WITHOUT \'L\' PLATES </option><option value=\"PL20\">PL20 DRIVING UNACCOMPANIED </option><option value=\"PL30\">PL30 CARRYING A PERSON NOT QUALIFIED </option><option value=\"PL40\">PL40 DRAWING UNAUTHORISED TRAILER </option><option value=\"PL50\">PL50 UNDEFINED PROVISIONAL OFFENCE </option><option value=\"SC19\">SC19 SECTION 19 (TRANSPORT ACT 1981) DISQUALIFICATION </option><option value=\"SC35\">SC35 SECTION 35 (TRANSPORT ACT 1981) DISQUALIFICATION </option><option value=\"SP10\">SP10 SPEEDING, GOODS VEHICLE </option><option value=\"SP20\">SP20 SPEEDING (NOT GOODS OR PASSENGER) </option><option value=\"SP30\">SP30 EXCEEDING STATUTORY SPEED LIMIT </option><option value=\"SP40\">SP40 EXCEEDING PASSENGER VEHICLE SPEED LIMIT </option><option value=\"SP50\">SP50 EXCEEDING MOTORWAY LIMIT </option><option value=\"SP60\">SP60 UNDEFINED SPEEDING OFFENCE </option><option value=\"TS10\">TS10 TRAFFIC LIGHTS OFFENCE </option><option value=\"TS20\">TS20 DOUBLE WHITE LINE OFFENCE </option><option value=\"TS30\">TS30 STOP SIGN OFFENCE </option><option value=\"TS40\">TS40 TRAFFIC CONSTABLE OFFENCE </option><option value=\"TS50\">TS50 TRAFFIC SIGNS (EXCL LIGHTS, STOP, LINES) </option><option value=\"TS60\">TS60 SCHOOL CROSSING OFFENCE </option><option value=\"TS70\">TS70 UNDEFINED TRAFFIC SIGNS OR DIRECTION </option><option value=\"TT99\">TT99 TO SIGNIFY A DISQUALIFICATION UNDER \'TOTTING-UP\' PROCEDURE </option><option value=\"UT10\">UT10 TAKING AND DRIVING AWAY </option><option value=\"UT20\">UT20 STEALING A VEHICLE </option><option value=\"UT30\">UT30 GOING EQUIPPED TO STEAL OR TAKE AWAY </option><option value=\"UT40\">UT40 TAKING, RIDING, KNOWINGLY DRIVING </option><option value=\"UT50\">UT50 AGGRAVATED TAKING OF A VEHICLE </option><option value=\"XX99\">XX99 TO SIGNIGY A DISQUALIFICATION UNDER \'TOTTING-UP\' PROCEDURE </option></select><br /><p id=\"conviction_Error0\" class=\"errorField\">Test</p><label class=\"description\" for=\"numberPoints\">Number of points </label><br /><select id=\"numberPoints_0\" name=\"convictionDetails[0][numberPoints]\"><option value=\"0\">0 </option><option value=\"1\">1 </option><option value=\"2\">2 </option><option value=\"3\">3 </option><option value=\"4\">4 </option><option value=\"5\">5 </option><option value=\"6\">6 </option><option value=\"7\">7 </option><option value=\"8\">8 </option><option value=\"9\">9 </option><option value=\"10\">10 </option><option value=\"11\">11 </option><option value=\"12\">12 </option><option value=\"13\">13 </option><option value=\"14\">14 </option><option value=\"15\">15 </option><option value=\"16\">16 </option><option value=\"17\">17 </option><option value=\"18\">18 </option><option value=\"19\">19 </option><option value=\"20\">20 </option></select><br /><p id=\"numberPoints_Error0\" class=\"errorField\">Test</p><label class=\"description\" for=\"convictionDate\">Date of Conviction</label><br /><input id=\"convictionDate_0\" name=\"convictionDetails[0][convictionDate]\" class=\"element text medium  convictionDate\" size=\"10\" maxlength=\"10\" value=\"\" type=\"text\"><br /><p id=\"convictionDate_Error0\" class=\"errorField\">Test</p><a class=\"remove_field\" href=\"#\" style=\"color:red;\">Remove [x]</a></div>");

$replace1o = "convictionDate_";
$replace1r = "convictionDate_+counter+";
str_replace($replace1o,$replace1r,$string);
$replace2o = "convictionDetails[0]";
$replace2r = "convictionDetails[+counter+]";
str_replace($replace2o,$replace2r,$string);

Open in new window



str_replace isn't replacing any of the strings.
0
Comment
Question by:Alex Mac
  • 2
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
Chris Harte earned 500 total points
Comment Utility
Your string is missing quotes.
$string = "<div style=\"margin-bottom:10px;\"><label class=\"description\" for=\"conviction\">Conviction </label><br /><select id=\"conviction_0\" name=\"convictionDetails[0][conviction]\"><option value=\"AC10\">AC10 FAILING TO STOP AFTER AN ACCIDENT </option><option value=\"AC12\">AC12 AIDING AND ABETTING (RELATING TO AC10) </option><option value=\"AC14\">AC14 CAUSING OR PERMITTING (RELATING TO AC10) </option><option value=\"AC20\">AC20 FAILING TO GIVE PARTICULARS OR REPORT IN 24HR </option><option value=\"AC30\">AC30 UNDEFINED ACCIDENT OFFENCES </option><option value=\"BA10\">BA10 DRIVING WHILE DISQUALIFIED BY ORDER OF COURT </option><option value=\"BA20\">BA20 DRIVING WHILE DISQUALIFIED AS UNDER AGE </option><option value=\"BA30\">BA30 ATTEMPTING TO DRIVE WHILE DISQUALIFIED </option><option value=\"CD10\">CD10 DRIVING WITHOUT DUE CARE AND ATTENTION </option><option value=\"CD20\">CD20 DRIVING WITHOUT REASONABLE CONSIDERATION </option><option value=\"CD30\">CD30 DRIVING WITHOUT DUE CARE & ATTENTION </option><option value=\"CD40\">CD40 CAUSING DEATH - DRIVING WHEN UNFIT - DRINK </option><option value=\"CD50\">CD50 CAUSING DEATH - DRIVING WHEN UNFIT - DRUGS </option><option value=\"CD60\">CD60 CAUSING DEATH - DRIVING OVER ALCOHOL LIMIT </option><option value=\"CD70\">CD70 CAUSING DEATH - DRIVING & SPECIMEN REFUSED </option><option value=\"CU10\">CU10 DEFECTIVE BRAKES </option><option value=\"CU20\">CU20 DANGEROUS USE (NOT BRAKES, STEERING OR TYRES) </option><option value=\"CU30\">CU30 DEFECTIVE TYRES </option><option value=\"CU40\">CU40 DEFECTIVE STEERING </option><option value=\"CU50\">CU50 DANGEROUS LOAD </option><option value=\"CU60\">CU60 UNDEFINED CONSTRUCTION AND USE FAILURE </option><option value=\"CU80\">CU80 DRIVING WHILST USING A MOBILE PHONE </option><option value=\"DD10\">DD10 DANGEROUS DRIVING </option><option value=\"DD20\">DD20 DANGEROUS SPEED </option><option value=\"DD30\">DD30 RECKLESS DRIVING </option><option value=\"DD40\">DD40 COMBINATION OF DANGEROUS, SPEED OR RECKLESS </option><option value=\"DD50\">DD50 DEATH BY DANGEROUS DRIVING </option><option value=\"DD60\">DD60 CULPABLE HOMICIDE WHILE DRIVING </option><option value=\"DD70\">DD70 DEATH BY RECKLESS DRIVING </option><option value=\"DD80\">DD80 CAUSING DEATH BY DANGEROUS DRIVING </option><option value=\"DR10\">DR10 DRIVING WITH ALCOHOL LEVEL ABOVE LIMIT </option><option value=\"DR20\">DR20 DRIVING WHILE UNFIT THROUGH DRINK </option><option value=\"DR30\">DR30 DRIVING THEN FAILING TO SUPPLY A SPECIMEN </option><option value=\"DR40\">DR40 IN CHARGE OF A VEHICLE - ALCOHOL ABOVE LIMIT </option><option value=\"DR50\">DR50 IN CHARGE OF A VEHICLE - UNFIT THROUGH DRINK </option><option value=\"DR60\">DR60 FAILURE - PROVIDE SPECIMEN OTHER THAN DRIVING </option><option value=\"DR70\">DR70 FAILING TO PROVIDE SPECIMEN FOR BREATH TEST </option><option value=\"DR80\">DR80 DRIVING WHEN UNFIT THROUGH DRUGS </option><option value=\"DR90\">DR90 IN CHARGE OF VEHICLE WHEN UNFIT THROUGH DRUGS </option><option value=\"IN10\">IN10 DRIVING UNINSURED </option><option value=\"IN12\">IN12 AIDING AND ABETTING (RELATING TO IN10) </option><option value=\"IN14\">IN14 CAUSING OR PERMITTING (RELATING TO IN10) </option><option value=\"LC10\">LC10 DRIVING WITHOUT A LICENCE </option><option value=\"LC20\">LC20 DRIVING NOT IN ACCORDANCE WITH A LICENCE </option><option value=\"LC30\">LC30 DRIVING AFTER MAKING A FALSE DECLARATION </option><option value=\"LC40\">LC40 DRIVING FAILED TO NOTIFY OF A DISABILITY </option><option value=\"LC50\">LC50 DRIVING AFTER LICENCE REVOKED MEDICAL GROUNDS </option><option value=\"MS10\">MS10 LEAVING A VEHICLE IN A DANGEROUS POSITION </option><option value=\"MS20\">MS20 UNLAWFUL PILLION RIDING </option><option value=\"MS30\">MS30 PLAYSTREET OFFENCES </option><option value=\"MS40\">MS40 DRIVING - UNCORRECTED EYESIGHT/REFUSING TEST </option><option value=\"MS50\">MS50 MOTOR RACING ON THE HIGHWAY </option><option value=\"MS60\">MS60 OFFENCES NOT COVERED BY OTHER CODES </option><option value=\"MS70\">MS70 DRIVING WITH UNCORRECTED DEFECTIVE EYESIGHT </option><option value=\"MS80\">MS80 REFUSING TO SUBMIT TO AN EYESIGHT TEST </option><option value=\"MS90\">MS90 FAILURE TO GIVE INFORMATION AS TO DRIVER ID </option><option value=\"MT01\">MT01 DRIVING WHILST USING A MOBILE TELEPHONE </option><option value=\"MW10\">MW10 CONTRAVENING SPECIAL ROAD REGULATIONS </option><option value=\"NE99\">NE99 NON ENDORSABLE OFFENCE </option><option value=\"PC10\">PC10 PEDESTRIAN CROSSING (UNDEFINED) </option><option value=\"PC20\">PC20 MOVING VEHICLE AND PEDESTRIAN CROSSING </option><option value=\"PC30\">PC30 STATIONARY VEHICLE AND PEDESTRIAN CROSSING </option><option value=\"PL10\">PL10 DRIVING WITHOUT \'L\' PLATES </option><option value=\"PL20\">PL20 DRIVING UNACCOMPANIED </option><option value=\"PL30\">PL30 CARRYING A PERSON NOT QUALIFIED </option><option value=\"PL40\">PL40 DRAWING UNAUTHORISED TRAILER </option><option value=\"PL50\">PL50 UNDEFINED PROVISIONAL OFFENCE </option><option value=\"SC19\">SC19 SECTION 19 (TRANSPORT ACT 1981) DISQUALIFICATION </option><option value=\"SC35\">SC35 SECTION 35 (TRANSPORT ACT 1981) DISQUALIFICATION </option><option value=\"SP10\">SP10 SPEEDING, GOODS VEHICLE </option><option value=\"SP20\">SP20 SPEEDING (NOT GOODS OR PASSENGER) </option><option value=\"SP30\">SP30 EXCEEDING STATUTORY SPEED LIMIT </option><option value=\"SP40\">SP40 EXCEEDING PASSENGER VEHICLE SPEED LIMIT </option><option value=\"SP50\">SP50 EXCEEDING MOTORWAY LIMIT </option><option value=\"SP60\">SP60 UNDEFINED SPEEDING OFFENCE </option><option value=\"TS10\">TS10 TRAFFIC LIGHTS OFFENCE </option><option value=\"TS20\">TS20 DOUBLE WHITE LINE OFFENCE </option><option value=\"TS30\">TS30 STOP SIGN OFFENCE </option><option value=\"TS40\">TS40 TRAFFIC CONSTABLE OFFENCE </option><option value=\"TS50\">TS50 TRAFFIC SIGNS (EXCL LIGHTS, STOP, LINES) </option><option value=\"TS60\">TS60 SCHOOL CROSSING OFFENCE </option><option value=\"TS70\">TS70 UNDEFINED TRAFFIC SIGNS OR DIRECTION </option><option value=\"TT99\">TT99 TO SIGNIFY A DISQUALIFICATION UNDER \'TOTTING-UP\' PROCEDURE </option><option value=\"UT10\">UT10 TAKING AND DRIVING AWAY </option><option value=\"UT20\">UT20 STEALING A VEHICLE </option><option value=\"UT30\">UT30 GOING EQUIPPED TO STEAL OR TAKE AWAY </option><option value=\"UT40\">UT40 TAKING, RIDING, KNOWINGLY DRIVING </option><option value=\"UT50\">UT50 AGGRAVATED TAKING OF A VEHICLE </option><option value=\"XX99\">XX99 TO SIGNIGY A DISQUALIFICATION UNDER \'TOTTING-UP\' PROCEDURE </option></select><br /><p id=\"conviction_Error0\" class=\"errorField\">Test</p><label class=\"description\" for=\"numberPoints\">Number of points </label><br /><select id=\"numberPoints_0\" name=\"convictionDetails[0][numberPoints]\"><option value=\"0\">0 </option><option value=\"1\">1 </option><option value=\"2\">2 </option><option value=\"3\">3 </option><option value=\"4\">4 </option><option value=\"5\">5 </option><option value=\"6\">6 </option><option value=\"7\">7 </option><option value=\"8\">8 </option><option value=\"9\">9 </option><option value=\"10\">10 </option><option value=\"11\">11 </option><option value=\"12\">12 </option><option value=\"13\">13 </option><option value=\"14\">14 </option><option value=\"15\">15 </option><option value=\"16\">16 </option><option value=\"17\">17 </option><option value=\"18\">18 </option><option value=\"19\">19 </option><option value=\"20\">20 </option></select><br /><p id=\"numberPoints_Error0\" class=\"errorField\">Test</p><label class=\"description\" for=\"convictionDate\">Date of Conviction</label><br /><input id=\"convictionDate_0\" name=\"convictionDetails[0][convictionDate]\" class=\"element text medium  convictionDate\" size=\"10\" maxlength=\"10\" value=\"\" type=\"text\"><br /><p id=\"convictionDate_Error0\" class=\"errorField\">Test</p><a class=\"remove_field\" href=\"#\" style=\"color:red;\">Remove [x]</a></div>";

Open in new window


also change the variable $string to capture the changes

$string = str_replace  ($replace1o, $replace1r, $string);

Open in new window

0
 

Author Comment

by:Alex Mac
Comment Utility
Oh right so does string replace not actually modify the original string?
0
 
LVL 16

Expert Comment

by:Chris Harte
Comment Utility
No, that is just the haystack, you have to save the return value of str_replace.
0
 

Author Closing Comment

by:Alex Mac
Comment Utility
Thanks
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now