Jump to content

PHP String Search


sh0wtym3

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.