A Simple Preg_replace Help Please.

free web hosting
Open Discussion > CONTRIBUTE > Computers > Programming Languages > PHP Programming

A Simple Preg_replace Help Please.

apple
Hello..
Im looking for some help.

I want to use preg_replace function to replace the following type of code tags.
CODE
<code lang="php"></code>
<code lang="javascript"></code>
<code lang="css"></code>


My question is that, in the above code tags, language (lang) is not always same, how can i use preg_replace with the above code tags to place them with something.

Any help will be very much appreciated.
thanks.

Reply

truefusion
You would need preg_replace_callback() instead.

Here's an example code to get you started:
CODE
<?php

$str = '<code lang="php"></code>
<code lang="javascript"></code>
<code lang="css"></code>';

function parse_code($matches){
    if ($matches[1] == "php"){
        return "[ code ]".$matches[2]."[ /code ]";
    } else if ($matches[1] == "javascript"){
        return "[ code ]".$matches[2]."[ /code ]";
    } else if ($matches[1] == "css"){
        return "[ code ]".$matches[2]."[ /code ]";
    }
}

$str = preg_replace_callback("/<code lang=\"(\w+)\">(.*)<\/code>/", "parse_code", $str);
echo $str;

?>

 

 

 


Reply

gogoily
truefusion may make a mistake in 18th line, try this:
CODE
<?php

$str = '<code lang="php"></code>
<code lang="javascript"></code>
<code lang="css"></code>';

function parse_code($matches){
    if ($matches[1] == "php"){
        return "[ code ]".$matches[2]."[ /code ]";
    } else if ($matches[1] == "javascript"){
        return "[ code ]".$matches[2]."[ /code ]";
    } else if ($matches[1] == "css"){
        return "[ code ]".$matches[2]."[ /code ]";
    }
}

$str = preg_replace_callback("/<code lang=\"(\w+)\">(.*)<\/code>/", "parse_code", $matches);
echo $str;

?>

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. javascript regexp illegal characters - 376.66 hr back. (1)
  2. matches - 404.45 hr back. (1)
  3. preg_replace illegal characters - 799.96 hr back. (1)
  4. detect links preg_replace - 929.21 hr back. (1)
  5. preg_replace funky characters - 1005.36 hr back. (1)
Similar Topics

Keywords : simple, preg, replace,

  1. Preg Replace Problem
    (1)
  2. Php Preg Replace
    (1)
    Ive got a problem with preg replace, this is the code: display.php CODE <table
    border="1"> <tr><th width="70">Time</th><th
    width="200">IP</th><th
    width="70">Viewed</th></tr> <?php $LogData =
    file_get_contents("log.txt"); $Find =
    "/||(.*)|(.*)|(.*)||/i"; $Replace =
    "<tr><td>$1</td><td>$2</td><td>$3
    </td></tr>"; &#....
  3. Validation Script - Detecting Illegal Characters
    preg_match to find illegal characters (0)
    I'm trying to validate a user's desired username for a registration page. I want to detect
    any illegal characters being used for a username. So far i have managed to include most execpt the
    '\', the '\' is used for escape and treating the preceding characters
    literally. I try to do '\\' but i get an error because it execects another
    character. CODE $ck_result = "Default"; $pattern =
    "/[!|@|#|$|%|^|&|*|(|)|_|\-|=|+|\||,|.|\/|;|:|\'
    ;|\"|&#....
  4. Preg_replace Problems
    (7)
    Sigh... preg_replace is getting very annoying for me. Could sombody tell me what I'm doing
    wrong? Im making a bbcode replace for the forums I made, and preg_replace is being a b****...
    Right now Im working on the tag, and I want to check for valid URL's, because idiots in the
    forum are adding js and stuff in the image tags and messing it all up. So i put a function in the
    array that holds all of the replacements: CODE
    $main_search=array('/\[b\](.*?)\[\/b\]
    /is','/\[i\ ....
  5. Help With Preg_replace
    (3)
    I must be a total noob, or just really tired...............-.- either way, I cannot understand what
    exactly those funky strings preg_replace wants for the pattern. Could sombody explain to me exactly
    what the pattern string and the replace string in preg replace does? I get str_replace...
    that's quite basic. I considered myself a pro at php until tonight........haha shows how much i
    know. I bet tomorow, if i look it up I'll get it.... Anyway.. any help? Thanks.....
    /laugh.gif" style="vertical-align:middle" emoid=":lol:" border="0" alt="laugh.gif" />....
  6. Replace A Character In A Specific Tag
    (7)
    I need a code that replaces all spaces between ] brackets with an underscore. So, basically, I
    write this into a variable: CODE bla bla something [[some text]] more text here
    And I want a script to change it to this: CODE bla bla something [[some_text]]
    more text here Any suggestions?....
  7. Regexp Function Preg_match_all()
    preg_match_all() - Help me (0)
    Hi, I got a new problem which has caused me to go mad but no solution. preg_match_all() - is the
    problem. I have something like this: CODE [ol] [li]Test1[/li]
    [li]Test2[/li] [li]Test3[/li] [li]Test4[/li]
    [li]Test5[/li] [/ol] Some text.Some text.Some text. [ol]
    [li]Test1[/li] [ol] [li]Test1[/li]
    [li]Test2[/li] [li]Test3[/li] [li]Test4[/li]
    [li]Test5[/li] &....
  8. Help With Reading Files
    Read and replace/insert data from form (5)
    Hi, Does anyone know how i can do this, or scripts that will work on Trap17's servers and will
    do the following: I have a .doc file form. Which i want to have filled in automactically, by HTML
    Form and emailed to my address, with the data filled in. Any ideas? I have heard many differing
    things, like XML, RTF, DOC, PDF... I have serched through many places and come up empty handed with
    anything that works. /sad.gif' border='0' style='vertical-align:middle' alt='sad.gif' /> ....
  9. Replace Shtml With Php?
    I don't know jack.... (1)
    I don't know anything about PHP, except that I use phpBB and have modified my forums template
    files. I used to make web sites years ago and just got back into it a couple months ago. I wonder
    if I should stop using shtml and switch to php. I don't really have time to learn php (at least
    not yet), but if it's easy to replace my Server Side Include with php (just to be more "modern")
    I would be willing to do so. Anyone have any suggestions about me doing so? Am I'm too old
    school for using shtml? Lol /laugh.gif' border='0' style='vertical-align:middle' ....

    1. Looking for simple, preg, replace,

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for simple, preg, replace,

*MORE FROM TRAP17.COM*
advertisement



A Simple Preg_replace Help Please.



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE