Jump to content

[SOLVED] Checking for a string and replacing


Tonic-_-

Recommended Posts

Hello, it's been awhile since i've gotten serious with php and i'm wondering how can I check for a specific string in the a number of lengths submitted by a form.

 

Example:

 

The user submits something like

12345678

 

I want to check the 3rd length 4th length and 5th length of the submitted text of each line.

 

What I mean by each line is like a form with a textarea so they can submit each line of something..

 

Example:

12345678 yada yada blah blah

nextline hereyada yada blah blah

blahblah yadayada blah blah

 

It's been quite some time since i've done php and can't seem to figure this out. I considered of use of detecting a certain string of text but I want to check only a certain length of it instead of a string variable.

 

Now which if the string matches to the variables I have set how can I make it replace it with a new text or modifying the string..

Link to comment
Share on other sites

Well what I want to do (more clearly) is have users submit hex codes (values) through a textarea form and check a length of the string for EACH LINE to see if it equals to another string I have stored in a selected variable. I sorta figured out the checking part but always had troubles checking each line submitted by a textarea box.

 

Basically they submit stuff like

2040304 04040404

2040308 04040404

205030c 03030303

 

Check 204 and 205 for whatever my reason is and compare it to variables. I can do the actual check part just can't figure out how to read only the first 3 lengths of each line submitted by the text area.

 

If the first line matches the variable string i have set then display a checkmark or something beside the first line..

 

And basically continue the way I stated above for each line or whatever.

Link to comment
Share on other sites

<?php
if(isset($_POST['your-textarea'])) {
	$code_array = explode("\n", $_POST['your-textarea']);
	foreach($code_array as $hex) {
		if(substr($hex, 0, 2) == "you-value") {
			//there's a match
		}
	}
}

 

Something like that should do the job for you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.