Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strings (find, check, replace) 1

Status
Not open for further replies.

TommyB44

Technical User
Jun 16, 2005
76
0
0
GB
Hi All,

I'd like to check a sentence for an url and if found switch(replace) it to the appropriate HTML

To give you more of an idea something like this..

Code:
//if the start of the string equals "http" or "www"

if(string=='http:'||'[URL unfurl="true"]www.')[/URL]

//then check the end of the string

 case 'gif':case 'jpg':case 'jpeg':case 'png':case 'bmp':
   result='<img src="'+x+'">';break;
 case 'wav':case 'au':case 'mid':case 'aif':case 'ram':case 'mp3':case 'aiff':case 'wma':case 'asf':
   result=<bgsound src='x'>;break;
 case 'swf':case 'mpg':case 'mpeg':case 'm2p':case 'mp4':case 'qt':case 'vob':case 'rm':case 'avi':case 'wmv':case 'mov':case 'asx':
   result=<embed src='x' autostart="true" loop="false" width=640 height=480></embed>;break;
 default:
   result='<a href='+x+' style="cursor:hand">'+x+'</a>';


none of that is even close to working right now, it's just how I think it might look.
 
Try:
Code:
string = string.toLowerCase();
if(string.indexOf('http:')==0 || string.indexOf('[URL unfurl="true"]www.')==0)[/URL]
  {
  endstring = string.substr(string.lastIndexOf('.') + 1);
  switch (endstring)
    {
    case 'gif':
    case 'jpg':
    case 'jpeg':
    case 'png':
    case 'bmp':
      {result='<img src="'+x+'">';break;}
    case 'wav':
    case 'au':
    case 'mid':
    case 'aif':
    case 'ram':
    case 'mp3':
    case 'aiff':
    case 'wma':
    case 'asf':
      {result=<bgsound src='x'>;break;}
    case 'swf':
    case 'mpg':
    case 'mpeg':
    case 'm2p':
    case 'mp4':
    case 'qt':
    case 'vob':
    case 'rm':
    case 'avi':
    case 'wmv':
    case 'mov':
    case 'asx':
      {
      result='<embed src="' + x + '" autostart="true" loop="false" width=640 height=480></embed>';
      break;
      }
    default:
     {result='<a href='+x+' style="cursor:hand">'+x+'</a>';)
    }
  }
Lee
 
Thanks Lee,

Would I need to add " string = string.split(" "); " to check every word in the sentence, or is there a better way.?
 
What I've tried so far with the html that might give you more of an idea. I haven't got it to work yet, I'm probably doing loads of things wrong, I don't mind people telling me what they are, it's the only way I'll learn.

Code:
<script>
function callcon(){

document.getElementById("input").value = string;
x = split.string(" ");
x = x.toLowerCase();

if(x.indexOf('http:')==0 || x.indexOf('[URL unfurl="true"]www.')==0)[/URL]
  {
  endstring = x.substr(x.lastIndexOf('.') + 1);
  switch (endstring)
    {
    case 'gif':
    case 'jpg':
    case 'jpeg':
    case 'png':
    case 'bmp':
      {result='<img src="'+x+'">';break;}
    case 'wav':
    case 'au':
    case 'mid':
    case 'aif':
    case 'ram':
    case 'mp3':
    case 'aiff':
    case 'wma':
    case 'asf':
      {result=<bgsound src='+x+'>;break;}
    case 'swf':
    case 'mpg':
    case 'mpeg':
    case 'm2p':
    case 'mp4':
    case 'qt':
    case 'vob':
    case 'rm':
    case 'avi':
    case 'wmv':
    case 'mov':
    case 'asx':
      {
      result='<embed src="' + x + '" autostart="true" loop="false" width=640 height=480></embed>';
      break;
      }
    default:
     {result='<a href='+x+' style="cursor:hand">'+x+'</a>';)
    }
}
document.getElementById("output").innerHTML = string;
document.getElementById("input").value = "";
  }
</script>


<form name="main" id="main">

<input type="text" style="width:200px;height:200px" id="input"><br><br>
<button onclick="callcon();">Convert</button>

</form>
<div id="output" style="border:3px groove red;width:200px;height:200px">
</div>


Thanks for reading.
 
It looks like you need to start from the beginning with Javascript, take some tutorials, and build slowly. There are a number of problems with your programming logic, and providing code for you won't solve your real problems.

split returns an array of strings, yet you try to use it like it's not an array.

Where did you get the original string variable contents from? I'd guess that you have the first line in your function backwards.

input isn't a good id to use for any container.

These errors are a good indication that you probably need to work with some things that are less complex, learn the language and syntax and practice more before tackling the project you've presented here.

Lee

 
I noticed the

Code:
string = document.getElementById("input").value;

after I posted it.

I changed it to "input", "output" to try and help show that I was converting one input to another output, I'd never normally use them as ID's or Name's, The original string was taken and from and changed, it's true I know nothing of switch in javascript but I use select in asp and vb all the time, I'm not a javascript expert as you can see and have never claimed to be, thanks for the advice, on a side note, I wouldn't have thought this was so complex, maybe more complex for me, knowing less about javascript.
 
it should also be "string.split(" ");
 
document.getElementById("input").value = string;
x = split.string(" ");
x = x.toLowerCase();

if(x.indexOf('http:')==0 || x.indexOf('
The above code, copied from your last example, assigns a string to an input but the rest of your code doesn't show that the string variable has any value assigned to it. Then you split the string into an array, and try to use a String method on an Array. You subsequently try to access the array again as a String in the if statement.

Strings and Arrays are not the same things in VBScript (which I use, too) or Javascript, and functions and methods from one type of object can't be used on the other like the code here attempts.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top