• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 367
  • Last Modified:

Convert PHP string replace function to ASP

I'm trying to replicate a PHP string replace function in ASP but I'm having problems completing it. I've attached two working PHP functions that seem to do the same thing, and my attempt at an ASP version. Please run the PHP to see what I'm looking for or view the following for more info http://www.madimmarketing.com/internet-marketing/php-text-spinner/ 
<?php

function spinner1($text) {
    $txt = preg_split("/{|}/", $text);
    foreach($txt as $key => $t){
        if($key%2){
            $spin = preg_split("/\|/", $t);
            $txt[$key] = $spin [mt_rand(1,count($spin)-1)];
        }
    }
    $string = implode("", $txt);
    return $string; 
}  

$test = "The {quick|slow|reasonably paced} {brown|green|blue|pink} {fox|goat|rat|camel} {jumped|walked|hopped} {over|past|under} the {lazy|tired|boring} {dog|cat|stoat}";
echo spinner1($test."<br />");


function spinner2($s){
	preg_match('#\{(.+?)\}#is',$s,$m);
	if(empty($m)) return $s;

	$t = $m[1];

	if(strpos($t,'{')!==false){
		$t = substr($t, strrpos($t,'{') + 1);
	}

	$parts = explode("|", $t);
	$s = preg_replace("+\{".preg_quote($t)."\}+is", $parts[array_rand($parts)], $s, 1);

	return spinner2($s."<br />");
}

echo spinner2('{This|Here} is {some|a {little|wee} bit of} {example|sample} text.');

?>


<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%

function spin(str)
	response.Write(str)
	Set regEx = New RegExp
	regEx.IgnoreCase = true
	regEx.Global = true
	regEx.Pattern = "\{(.*?)\}"
	
	Set Matches = regEx.Execute(str)
	Response.write "# Spin tags:" & Matches.Count & "<br />--------------<br />" 
	For Each Match in Matches
		sMatch = replace(Match.Value,"{","")
		sMatch = replace(sMatch,"}","")
		'response.Write("sMatch = " & sMatch & "<br />")
		a = Split(sMatch,"|")
		aTotal = uBound(a) + 1
		response.Write("words to spin: " & aTotal & "<br />")
		Randomize
		i = Int(Rnd * aTotal)+1
		response.Write("random: " & i & "<br />")
		response.write("output: " & a(i-1) & "<br />--------------<br />")
		
		i = ""
	Next
	spin = str
end function

spin "The {quick|slow|reasonably paced} {brown|green|blue|pink} {fox|goat|rat|camel} {jumped|walked|hopped} {over|past|under} the {lazy|tired|boring} {dog|cat|stoat}"
 
%>

Open in new window

0
benners70
Asked:
benners70
  • 2
1 Solution
 
käµfm³d 👽Commented:
Try this:

Spinner 1
Function spinner1(text)
        Const placeholder = "%%%##%%%"
        
        Set regex = New RegExp
        
        With regex
                .Pattern = "{|}"
                .Global = True
                fixed = .Replace(text, placeholder)
        End With
        
        txt = Split(fixed, placeholder)
        
        Randomize
        
        For key = 0 To UBound(txt)
                If key Mod 2 <> 0 Then
                        spin = Split(txt(key), "|")
                        idx = Int((UBound(spin) + 1)*Rnd(Timer))
                        txt(key) = spin(idx)
                End If
        Next
        
        str = ""
        
        For Each value In txt
                str = str & value
        Next
        
        spinner1 = str
End Function

Open in new window


Spinner 2
Function spinner2(s)
        Set regex = New RegExp
        
        With regex
                .Pattern = "\{([^}]*)\}"
                Set m = .Execute(s)
        End With
        
        If m Is Nothing Then
                spinner2 = s
                Exit Function
        ElseIf m.Count = 0 Then
                spinner2 = s
                Exit Function        
        End If
        
        t = m(0).SubMatches(0)
        
        If InStr(1, t, "{") > 0 Then
                t = Mid(t, InStr(1, t, "{") + 1)
        End If
        
        parts = Split(t, "|")
        
        ' Substitute for preg_quote
        With regex
                .Pattern = "([-^${}\[\]().?\\*+|])"
                .Global = True
                t = .Replace(t, "\$1")
        End With
        
        Randomize
        
        With regex
                .Global = False
                .Pattern = "\{" & t & "\}"
                idx = Int((UBound(parts) + 1)*Rnd(Timer))
                s = .Replace(s, parts(idx))
        End With
        
        spinner2 = spinner2(s & "<br />")

End Function

Open in new window

0
 
benners70Author Commented:
Perfect! Thanks very much.
0
 
käµfm³d 👽Commented:
Sure. Glad it worked for you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now