Jump to content

PHP String Search


sh0wtym3

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.