Solved

search and replace using regexp

Posted on 2008-10-12
5
511 Views
Last Modified: 2012-05-05
Hi i need some help on this one, i use a wysiwyg editor, but the editor is not able to show <% %> tags in wysiwyg, so insted i insert an image containg an ALT text.

e.g.: <img alt="!moduleStart!contactform(79)!moduleEnd!" src="/admin/editor/scripts/module.gif" />

So when i post the data to my database i want to replace this tag with:

<% call contactForm(79) %>

I worked out a solution with a simple replace function but the hurdle is i can have multiple images in the wysiwyg editor and theese were not replaced.

I tried to worksomething out look the code snippet, but i cant figure it out...


webSiteContent = Request.Form("inpContent")

        'this sub finds the matches

        Sub RegExpTest(strMatchPattern, strPhrase)

            'create variables

            Dim objRegEx, Match, Matches, StrReturnStr

            'create instance of RegExp object

            Set objRegEx = New RegExp

            'find all matches

            objRegEx.Global = True

            'set case insensitive

            objRegEx.IgnoreCase = True

            'set the pattern

            objRegEx.Pattern = strMatchPattern

            'create the collection of matches

            Set Matches = objRegEx.Execute(strPhrase)

            'print out all matches

            For Each Match in Matches

                strReturnStr = "Match found at position "

                strReturnStr = strReturnStr & Match.FirstIndex & "." & Match

                'Value is '"

                strReturnStr = strReturnStr & Match.value & "'."

                'print

                Response.Write(strReturnStr & "<BR>")

            Next

        End Sub

        'call the subroutine

        regExpTest "!moduleStart!([\s\S]+?)!moduleEnd!.", webSiteContent

Open in new window

0
Comment
Question by:EliteWeb
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:ddrudik
ID: 22697061
With the content:
<img alt="!moduleStart!contactform(79)!moduleEnd!" src="/admin/editor/scripts/module.gif" />
<img alt="!moduleStart!contactform(80)!moduleEnd!" src="/admin/editor/scripts/module.gif" />
<img alt="!moduleStart!contactform(81)!moduleEnd!" src="/admin/editor/scripts/module.gif" />
<img alt="!moduleStart!contactform(82)!moduleEnd!" src="/admin/editor/scripts/module.gif" />

And the ASP Code:
<%
Set regEx = New RegExp
regEx.Global = True
sourcestring = "your source string"
regEx.Pattern = "!moduleStart!([\s\S]+?)!moduleEnd!."
Set Matches = regEx.Execute(sourcestring)
  For z = 0 to Matches.Count-1
    results = results & "Matches(" & z & ") = " & chr(34) & Server.HTMLEncode(Matches(z)) & chr(34) & chr(13)
    For zz = 0 to Matches(z).SubMatches.Count-1
      results = results & "Matches(" & z & ").SubMatches(" & zz & ") = " & chr(34) & Server.HTMLEncode(Matches(z).SubMatches(zz)) & chr(34) & chr(13)
    next
    results=Left(results,Len(results)-1) & chr(13)
  next
Response.Write "<pre>" & results
%>

I get:
Matches(0) = "!moduleStart!contactform(79)!moduleEnd!""
Matches(0).SubMatches(0) = "contactform(79)"
Matches(1) = "!moduleStart!contactform(80)!moduleEnd!""
Matches(1).SubMatches(0) = "contactform(80)"
Matches(2) = "!moduleStart!contactform(81)!moduleEnd!""
Matches(2).SubMatches(0) = "contactform(81)"
Matches(3) = "!moduleStart!contactform(82)!moduleEnd!""
Matches(3).SubMatches(0) = "contactform(82)"
0
 

Author Comment

by:EliteWeb
ID: 22697083
Yes but i need the output as :

<% call contactForm(79) %>, so the images is replaced with functions...
0
 
LVL 27

Accepted Solution

by:
ddrudik earned 125 total points
ID: 22697891

<%

Set regEx = New RegExp

regEx.Global = True

sourcestring = "your source string"

regEx.Pattern = "<img alt=""!moduleStart!([\s\S]+?)!moduleEnd!""[^>]*>"

replacementstring = "<% call $1 %>"

Response.Write regEx.Replace(sourcestring,replacementstring)

%>

Open in new window

0
 

Author Comment

by:EliteWeb
ID: 22698025
Hi,

Not 100% :-)

Microsoft VBScript compilation error '800a0409'

Unterminated string constant

/admin/editor/loadEditor.asp, line 44

replacementstring = "<% call $1

But this works.

	Set regEx = New RegExp

		regEx.Global = True

		sourcestring = sNewHTML

		regEx.Pattern = "<img alt=""!moduleStart!([\s\S]+?)!moduleEnd!""[^>]*>"

		replacementstring = "<% call $1 %" & ">"

		sNewHTML = regEx.Replace(sourcestring,replacementstring)

Open in new window

0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22698644
Thanks for the question and the points, if there's anything else let me know.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now