A Simple Preg_replace Help Please.

free web hosting
Free Web Hosting, No Ads > 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.
Confirm Code:

Recent Queries:-
  1. preg_replace illegal characters - 58.06 hr back. (1)
  2. detect links preg_replace - 187.32 hr back. (1)
  3. preg_replace funky characters - 263.47 hr back. (1)
Similar Topics

Keywords : simple, preg, replace,

  1. Flash Tutorial Simple Motion Tween
    Introduction to motion tween (0)
  2. Create Dynamic Html/php Pages Using Simple Vb.net Code
    Taking your application data, and creating a webpage for others to vie (1)
    This example will show you how use a string in VB to create PHP code. In order to do this, you need
    a string to store your PHP page and a function that I will list at the bottom of the page for you to
    put in a module. This code is written in VB.NET Public Sub CreatePage(ByVal HTMLTitle As
    String, ByVal HTMLText As String, ByVal HTMLFileName As String) Dim strFile As String '
    ---------------------- ' -- Prepare String -- ' ---------------------- strFile = "" '
    -------------------- ' -- Write Starter -- ' -------------------- strFile = " " ....
  3. Finding Good Free Hosting Can Be Frustrating And Costly.
    Simple free hosting is anything but. It could cost you dearly. (1)
    I have a home. After several weeks of searching, reviewing, and posting I have solid, free, feature
    rich hosting. Thank God for Trap 17. The first question a reasonable person would ask is why bother?
    Just get paid hosting. Well I have paid hosting and it is not so different from free hosting. Same
    issues, same features. One big difference is when they go under your payment for services goes with
    them. I needed a good free host for my clients. I wanted to be able to say hey try this host with
    your new site from me and you can have great service at low or no cost. The b....
  4. Making A Simple Signature With Adobe Photoshop
    A tutorial to basic signature designing: By Accure (3)
    Specs Hardness: 4 / 10 Time: about 10 minutes Result: My .PSD File: Download it now!
    Needings: -Basic Photoshop knowlegend. -Adobe Photoshop CS2 (Or CS3/CS4) -Brushes set ( available
    here ) -A render ( available here ) Basic knowlegend: Steps: 01. Open Adobe Photoshop,
    and go File > New. 02. select the sizes Width: 350 pixels . Height: 150 pixels . Press OK .
    03. Press the button to maximize the window of your new project. 04. Fill your background with
    black (by using the "fill" function). 05. Press F5, click on the small arrow a....
  5. Simple Is Beter - The Future Of Computers?
    In the future, what will computers be like? (3)
    -Computers - The future - Who knows - I often wonder what the average home
    computer will be like in ten or twenty years time; They seem to get larger, and have more functions,
    be faster, and have more RAM ect. People always look for speed when buying a computer - But is this
    really the way forward? If you have formatted your hard drive, then re-installed your operating
    system; you will have noticed, that things run very smooth, very fast, and with few or no error
    messages. But as soon as you have installed all your devices, media software, some games m....
  6. Preg Replace Problem
    (1)
    If i have a word like c.........a......t (with the dots in) and i wanted to replace it with cat how
    would i do it. If i use CODE <?php $patterns =
    "/c(*)a(*)t/"; $replace = "cat"; echo
    preg_replace($patterns, $replace, 'c.....a.........t');
    [/color][color="#000000"]?> then when i type something like "
    c ome a nd look at t his" is also filtered. Is
    there any way just to filter cat with dots in? ....
  7. Unable To Replace .png File In Directory
    Can't even delete it using Cpanel (7)
    I can't delete a little file using file manager in Cpanel. I'm trying to delete a .png
    image, actually, I'm trying to replace it. The response comes back as a success, but when I
    reload my site it's still there. Then I go back to the file manager and go up a level, then back
    and WOOP! There it is! I'm beyond frustrated here. What is going on?....
  8. 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>"; &#....
  9. Verifying Your Email Address
    A Simple PHP Tutorial. (0)
    Hi, everyone. I'm offering the next PHP tutorial that verifies an email address of an user who
    wants to register on your site. This is how it works. When someone, went to registration page and
    filled out all of the details such as Name, Password, email and so on and submit those details to a
    server. The user is getting unique registration code to his or her email account, the one has to be
    clicked to activate the account. 1. Outline: a) You need to create or build the following files:
    -config.php (The configuration file) -signup.php (Initial Registration) ....
  10. Simple Javascript And Password System
    How to protect your pages with password (6)
    The quickest way to get a password protection system up and running is to use a Prompt box in
    JavaScript that has a title like "Enter your Email Address". Only you and the relevant users know
    what the password should be, could even be one each, that can be sorted out at the next page then
    pass the "input" directly through the url by changing the .href, like
    http://www.iSource.net.nz/users/?leTmeIn= The page that then processes this should also check for
    the referring page, and three fails from an IP if you like the php (the next page): CODE
    <?php // processdo....
  11. Php Replace Help!
    (6)
    CODE <?php function code( $input ){ $data[0] = "<?php
    include(\"shoutbox.php?no="; $data[1] = "\");
    ?>"; $find[0] =
    "/\[b\](.*?)\[\/b\]/i";
    $find[1] =
    "/\[url\=(.*)\](.*)\[\/url\]/"
    ; $find[2] =
    "/\[u\](.*?)\[\/u\]/i";
    $find[3] = "/\&....
  12. A Simple Attack System
    A how-to on making a simple attack system (0)
    ----------------------- Game Making ::Simple Attack Script:: Ok, When making my first, ok well
    second ..ok , my current and third one (first 2 were really bad, but then i found out my own way on
    how to make an idea script), I would always first write down or think of the stats of the character.
    For the purpose of this tut, i will be using space-ship kinda stats(since my first proper attack
    script was a space battle script) : The Attack Stats : -Accuracy(Aim) -Hull Damage(Main Dmg.)
    Defense Stats : -Shield Power(Armor) -Speed(dodge/swiftness) ATTACK/DAMAGE ....
  13. Simple Cms To Internet Marketing
    marketing seo (2)
    hey do you guys got some simple cms to make lots of sites quickly? maybe gonna with the collection
    functions? /wink.gif" style="vertical-align:middle" emoid=";)" border="0" alt="wink.gif" /> there
    i got some and let you know,anyone know more just reply, chyrp, it light weight,but it cost more
    cpu and memory. flatpress cool,no need to database textpattern i can not pass the w3 validator
    with it. /dry.gif" style="vertical-align:middle" emoid=" ....
  14. A Simple Cms?
    (6)
    I wanna to have a look at a simple CMS coding that can be use for my general studies in PHP. If you
    know any, please let me know.....
  15. How Often Do You Use Css?
    As opposed to using simple <font> and <div> tags (12)
    CSS is basically made to make your html coding a heck of a lot easier. However, sometimes I prefer
    to use html instead of CSS since it sometimes gets frustrating to refer to the same old style sheet
    and use an old font ID you made about a week ago. I find it a lot more efficient to just type out
    the specified font color/style/size in the tag than to use an individual or for certain fonts
    you'd like to use. Sure, it's very helpful a lot of the time to use CSS in most cases, since
    it's kind of like a very simplified version of PHP but in a different case,....
  16. A Simple Php Captcha - Image Validation
    (21)
    OK, I recently had the need to create a PHP CAPTCHA system for a friend, and I am sharing this as a
    tutorial with the good people here at Trap17. I am sure you have all seen a CAPTCHA before (although
    you may not have known what it was called). They are the little codes you often have to enter when
    you register with a site, to make sure you are a person and not an automated script. Some common
    examples look something like this: My system doesn't really do anything as fancy, but I
    think that it is slightly more readable that some of those that get generated. Every....
  17. Pci Simple Communications Controller / Multimedia Audio Controller
    No Audio (5)
    Hi. I'm Wilfred@cav. Please help me fix my desktop. I reformatted from Home XP to Pro XP. After
    the formatting, there was no sound and I have this 2 yellow-icon things (PCI Simple Communications
    Controller and Multimedia Audio Controller) on the device manager, which says that I don't have
    a driver installed for this device. I don't know what to do. Please help. I'm now at work
    and the desktop is in my place and I don't have internet access in my place. I'll appreciate
    any help. Thanks.....
  18. Dialup Users Double Your Connection Speed
    with simple modem command tweek (6)
    I found this instruction on the net and I'm wondering this actually "speeds up" a dial up
    connection? QUOTE Suffering from 52 kbps internet connection? Follow these simple steps and
    double your internet connection speed: Connect to the internet Click Start and select control
    panel Click Phone and Modem Select Modems tab and then click Properties Click the Advanced tab
    and in the extra initialization commands type AT&FX Click Ok and disconnect from the internet
    Connect to the internet again and enjoy your 115 kbps connection! Source http....
  19. What Is God?
    simple question, hard to answer! (52)
    Yeah this is the only thing I'd like to know! Is he a big old man with a beard somewhere
    above us (interstingly what is UP on one side of the earth is DOWN on the other side of it - so
    where is God then if we point up there?)??? Is he inside of us? Are we all a part of god? Is God
    present in everything that surrounds us? Isn't it interesting that GOD is very close to GOOD?
    Maybe it is a metaphore for being and doing GOOD things?? That is something we have inside of us -
    every normal human being has this feeling for what is good and bad and is tryin' to f....
  20. Watermark Your Image With Simple Php Script
    found it on the net (35)
    This script was found on the net http://tips-scripts.com/?tip=watermark#tip B&T's Tips &
    Scripts site. Just in case the site may not show, I will include the code here: List of things
    needed: 1. your image in any format 2. watermark image--in gif format with transparent background 3.
    script below with name (i.e. watermark.php) CODE <?php // this script creates a watermarked
    image from an image file - can be a .jpg .gif or .png file // where watermark.gif is a mostly
    transparent gif image with the watermark - goes in the same directory as this script // ....
  21. Simple C File Handling In Action
    Small code snipet which covers most of basic file handling and navigat (3)
    Yesterday I suddenly got a lot of work. The same work we try to push off, yes you are right all
    formalities to get the code review incorporated and update all source code files with code review
    headers. Imagine if you need to open 300 files one by one and append code review headers at the
    end. Since most files are reviewed in groups of 20 to 30 files. We require one header to be placed
    in say 20 to 30 files. To simplify I went back to my class assignment days and wrote this small c
    utility to open all files passed on command line and open attach code review headers an....
  22. How To: Make A Simple Php Site
    Making one file show up on all pages using php (21)
    I have looked all over the site and could not find anything that was like this simple, or just like
    this at all.. For some people i know that you are using a basic HTML site...and having a big menu
    if you want to add somthing you have to go into every one of the pages and add or remove or edit
    what you want to do, but with somthing verry simple all you would have to do is edit one file, and
    all of the pages that have the PHP script on them would suddenly change to what that one file is.
    So to start off if you are planning on using this little tirck, the page that you a....
  23. Simple Sig Tutorial
    photoshop sig tutorial : New to photoshop? (24)
    Mytutorial which i made for people who are new in photoshop: This is my first tutorial so maybe
    there are few mistakes .......
  24. Verifying Email Addresses
    Using a simple PHP script (9)
    This simple script will allow you to run some basic checks to make sure that any email address
    entered is actually an email address. There is no guarantee offered that this will stop every single
    fake email address, but it'll provide some protection. Now, the code! First we need to get
    the email address to verify. Here, I get it using POST from an HTML form. CODE <?php //Load
    email address from web form $email = $_POST['email']; Now, we move on
    to our first check. Does the text that has been entered look like it could b....
  25. Simple Login In Visual Basic 6
    user interaction example trough login programm (6)
    First of all, I am NOT a programmer, this is something my friend taught me. It describes basic
    interaction with the user, while showing basic functionality of this simple programm. So, without
    further ado, we're off to the tutorial: First of all, start your visual basic, when prompted
    for new project, select Standard Exe . Next, we need to open code window, so we can start typing
    the program. This can be done in two ways, one is double clicking on the form, or selecting Code
    from View menu. If you double clicked on the form, you will see following text: CODE ....
  26. Msgbox
    Here is a simple way and lists of MsgBoxes. (17)
    Here are lists of MsgBoxes: Syntax: MsgBox Prompt, MsgBoxStyle, Title, HelpFile MsgBox "Uh,
    text!", , "Uh, Title!" MsgBox "Uh, text!", vbCritical, "Uh, Title!" MsgBox "Uh,
    text!", vbExclamation, "Uh, Title!" MsgBox "Uh, text!", vbInformation, "Uh, Title!"
    MsgBox "Uh, text!", vbOKCancel, "Uh, Title!" MsgBox "Uh, text!", vbQuestion, "Uh,
    Title!" MsgBox "Uh, text!", vbYesNo, "Uh, Title!" MsgBox "Uh, text!", vbYesNoCancel,
    "Uh, Title!" MsgBox "Uh, text!", vbRetryCancel, "Uh, Title!" MsgBox "Uh, text....
  27. need a good and simple php-nuke tutorial
    (17)
    hello everybody i'm new to php-nuke , after installing it on my server i can't understand how the
    structure of the system works ,i have been looking at some tutorials about php-nuke but some of them
    were written like i know the system and what they are explainning is like a reminder of some kind
    can some one help me to find good and simple php-nuke tutorial. thanx....
  28. Php Mod Rewrite Tutorial
    Simple tutorial (3)
    REQUIRED: Internet server (Apache 1.3.33 recomended), php, basic skills with .htaccess Mod
    rewrite is cool Apache option, it helps you to make your site URLs simple and clean... also it is
    perfect for search engines... This is mod rewrite, it replaces : CODE
    site.com/index.php?include=files?ID=45 with: CODE site.com/files/45 so lets get startet,
    open notepad, enter this in it: CODE RewriteEngine On // - you are starting rewrite mod
    :) RewriteBase / // - you set the rewrite base RewriteRule
    ^(.+)/([0-9]+)/?$ in....
  29. How To Take A Screen Shot Of Your Desktop.
    A Simple Tutorial (22)
    Lots of people have asked me on how to capture a Screen Shot of their Desktop. So I thought I should
    make a tutorial on how to... 1. Press the print screen button which should be: 2. Open an image
    editing program like paint: 3. Press Ctrl+V (paste) 4. It should have pasted the Screen Shot,
    Now save your image. You learnt how to take a Screen Shot of your Desktop. By .::DAMAN::.....
  30. Php Simple Login Tutorial
    Learn how to make a simple login! (63)
    I have been quite busy lately, trying to design and code my site (far from done XD). And after
    having learned how to make a simple login, I will try to write my own tutorial, for you
    /smile.gif' border='0' style='vertical-align:middle' alt='smile.gif' /> the tutorial Step 1
    : The first step in designing a member system is to plan out exactly what you need. A common impulse
    among programmers is to jump right in and start coding. I'll be honest and admit that I'm
    guilty of this more so than anyone. However, since I'm in control of this conversation (y....

    1. Looking for simple, preg, replace,

Searching Video's for simple, preg, replace,
Similar
Flash
Tutorial
Simple
Motion Tween
-
Introduction
to motion
tween
Create
Dynamic
Html/php
Pages Using
Simple
Vb.net Code
- Taking
your
application
data, and
creating a
webpage for
others to
vie
Finding Good
Free Hosting
Can Be
Frustrating
And Costly.
- Simple
free hosting
is anything
but. It
could cost
you dearly.
Making A
Simple
Signature
With Adobe
Photoshop -
A tutorial
to basic
signature
designing:
By Accure
Simple Is
Beter - The
Future Of
Computers? -
In the
future, what
will
computers be
like?
Preg Replace
Problem
Unable To
Replace .png
File In
Directory -
Can't
even delete
it using
Cpanel
Php Preg
Replace
Verifying
Your Email
Address - A
Simple PHP
Tutorial.
Simple
Javascript
And Password
System - How
to protect
your pages
with
password
Php Replace
Help!
A Simple
Attack
System - A
how-to on
making a
simple
attack
system
Simple Cms
To Internet
Marketing -
marketing
seo
A Simple
Cms?
How Often Do
You Use Css?
- As opposed
to using
simple
<font>
and
<div>
tags
A Simple Php
Captcha -
Image
Validation
Pci Simple
Communicatio
ns
Controller /
Multimedia
Audio
Controller -
No Audio
Dialup Users
Double Your
Connection
Speed - with
simple modem
command
tweek
What Is God?
- simple
question,
hard to
answer!
Watermark
Your Image
With Simple
Php Script -
found it on
the net
Simple C
File
Handling In
Action -
Small code
snipet which
covers most
of basic
file
handling and
navigat
How To: Make
A Simple Php
Site -
Making one
file show up
on all pages
using php
Simple Sig
Tutorial -
photoshop
sig tutorial
: New to
photoshop?
Verifying
Email
Addresses -
Using a
simple PHP
script
Simple Login
In Visual
Basic 6 -
user
interaction
example
trough login
programm
Msgbox -
Here is a
simple way
and lists of
MsgBoxes.
need a good
and simple
php-nuke
tutorial
Php Mod
Rewrite
Tutorial -
Simple
tutorial
How To Take
A Screen
Shot Of Your
Desktop. - A
Simple
Tutorial
Php Simple
Login
Tutorial -
Learn how to
make a
simple
login!
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 more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
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