sh0wtym3 Posted April 7, 2010 Share Posted April 7, 2010 Given the code below: 1111 abcdefg 2222 3333 abcdefg 4444 5555 abcdefg 6666 Lets say I wanted to str_replace only the letters "cde" of the string "abcdefg" that lies in between "3333" and "4444" ... How can I do that? I have a feeling I might need to use regular expressions Link to comment https://forums.phpfreaks.com/topic/197918-php-string-search/ Share on other sites More sharing options...
br0ken Posted April 7, 2010 Share Posted April 7, 2010 You could either use a complex combination of substr, strpos and str_replace or use a regular expression function: http://php.net/manual/en/function.preg-replace.php Link to comment https://forums.phpfreaks.com/topic/197918-php-string-search/#findComment-1038558 Share on other sites More sharing options...
sh0wtym3 Posted April 7, 2010 Author Share Posted April 7, 2010 I'm trying but I'm not finding a viable solution, here's what I have: preg_replace("/cde/", "ccddee", $string); The problem is that "cde" appears multiple times, I need to get only the instance that appears between "3333" and "4444", if that makes sense. So once replaced, the new string will read: 1111 abcdefg 2222 3333 abccddeefg 4444 5555 abcdefg 6666 Link to comment https://forums.phpfreaks.com/topic/197918-php-string-search/#findComment-1038566 Share on other sites More sharing options...
br0ken Posted April 7, 2010 Share Posted April 7, 2010 Unfortunately I'm not great with regular expressions (I'm terrible actually!) but I know you can specify strings that need to appear before and/or after a variable. Hopefully someone else on here can shed some light on this for you. Link to comment https://forums.phpfreaks.com/topic/197918-php-string-search/#findComment-1038568 Share on other sites More sharing options...
sh0wtym3 Posted April 7, 2010 Author Share Posted April 7, 2010 Me too, I have been googling for hours! Link to comment https://forums.phpfreaks.com/topic/197918-php-string-search/#findComment-1038574 Share on other sites More sharing options...
sh0wtym3 Posted April 7, 2010 Author Share Posted April 7, 2010 To clarify.... I am basically using PHP to change the value of a CSS property, specifically "background-color". The problem is the background color property appears multiple times and I need to change the value of the one that appears inside a certain selector. Link to comment https://forums.phpfreaks.com/topic/197918-php-string-search/#findComment-1038598 Share on other sites More sharing options...
br0ken Posted April 7, 2010 Share Posted April 7, 2010 If I was doing something like that, I would parse the CSS into an array. Each element in the array would have the selector as the key and the values as the value. For example: <?php $css = array('body' => 'font-family:Arial;font-size:12px;', 'a' => 'color:#000;', 'a.big' => 'font-size:18px; font-weight:bold;', ); $selectorToFind = 'a.big'; $css[$selectorToFind] = str_replace('font-size:18px;', 'font-size:30px;', $css[$selectorToFind]); ?> Link to comment https://forums.phpfreaks.com/topic/197918-php-string-search/#findComment-1038603 Share on other sites More sharing options...
sh0wtym3 Posted April 7, 2010 Author Share Posted April 7, 2010 Would I be able to parse an entire stylesheet that way? I will be running different stylesheets through the script, and they are each about 450-500 lines long with tons of different selectors. From the example it seems like you defined your array manually, I would need the arrays to populate automatically. Then I could change that one property and rewrite the CSS file using fwrite() Link to comment https://forums.phpfreaks.com/topic/197918-php-string-search/#findComment-1038613 Share on other sites More sharing options...
br0ken Posted April 7, 2010 Share Posted April 7, 2010 I defined my array manually but you could parse yours dynamically easily enough. You could write this functionality manually using either substr and strpos or regular expressions. Alternatively, you could use one of the many pre-written PHP CSS parsers. http://www.google.co.uk/search?q=php+css+parser&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a Link to comment https://forums.phpfreaks.com/topic/197918-php-string-search/#findComment-1038635 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.