Destramic Posted December 19, 2006 Share Posted December 19, 2006 this is a function which is apart of my form validation script...basically i want to replace a selected<label.... and change the class (class="error")..i think i have the script sorted....just the regular expression if someone could please help?the text inbetween the <label></label> tags in undecided and i want to copy that value into the relaced script....hope that explains what i need that destramic[code] public function mark_label($input_name) { $filename = $_SERVER['SCRIPT_NAME']; ob_start(); require_once $document_root . $filename; $contents = ob_get_contents(); ob_end_clean(); $contents = str_replace("<label for=\"$input_name\">(.*)</label>", "<label for=\"$input_name\" class=\"\"></label>", $contents); return $contents; }[/code] Link to comment https://forums.phpfreaks.com/topic/31278-replace-string-function/ Share on other sites More sharing options...
obsidian Posted December 19, 2006 Share Posted December 19, 2006 I would use preg_replace() since str_replace() doesn't support regular expressions. Try something like this:[code]<?php$regex = "|(<label for\=\"$input_name\")(>)(.+?)(</label>)|";$replace = '$1 class="error"$2$3$4';$contents = preg_replace($regex, $replace, $contents);?>[/code]I just tested it with some test data, and it seems to work just fine. Link to comment https://forums.phpfreaks.com/topic/31278-replace-string-function/#findComment-144740 Share on other sites More sharing options...
Destramic Posted December 20, 2006 Author Share Posted December 20, 2006 thanks ive used what you have given me and changed it a bit...but i doesnt seem to workcan you have a look please?[code]<?php$contents = "<label for=\"test\">hello</label><input type=\"text\">";$regex = "|<label for=\"test\">(.+?)</label>|";$replace = "<label for=\"test\" class=\"error\">$1</label>"; $contents = preg_replace($regex, $replace, $contents);return $contents;?>[/code] Link to comment https://forums.phpfreaks.com/topic/31278-replace-string-function/#findComment-145410 Share on other sites More sharing options...
obsidian Posted December 20, 2006 Share Posted December 20, 2006 [quote author=Destramic link=topic=119293.msg489155#msg489155 date=1166647747]thanks ive used what you have given me and changed it a bit...but i doesnt seem to workcan you have a look please?[code]<?php$contents = "<label for=\"test\">hello</label><input type=\"text\">";$regex = "|<label for=\"test\">(.+?)</label>|";$replace = "<label for=\"test\" class=\"error\">$1</label>"; $contents = preg_replace($regex, $replace, $contents);return $contents;?>[/code][/quote]Try escaping your equal signs. Link to comment https://forums.phpfreaks.com/topic/31278-replace-string-function/#findComment-145413 Share on other sites More sharing options...
Destramic Posted December 20, 2006 Author Share Posted December 20, 2006 ok getting it to replace a string in a var works....but include the current form and changing a label doesnt seem to work...content:<label for"name">Name:</label>[code] $uri = $_SERVER['REQUEST_URI']; ob_start(); require_once $document_root . $uri; $contents = ob_get_contents(); ob_end_clean(); $regex = "|<label for=\"name\">(.+?)</label>|"; $replace = "<label for=\"name\" class=\"error\">$1</label>"; $contents = preg_replace($regex, $replace, $contents); return $contents;[/code] Link to comment https://forums.phpfreaks.com/topic/31278-replace-string-function/#findComment-145492 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.