I want remove some URL from user input.
Example user input
$input = 'http://www.yahoo.com/';
$input = 'http://yahoo.com/';
$input = 'www.yahoo.com';
$input = 'yahoo.com';
$input = 'http://answers.yahoo.com/question/index;_ylt=AhiI2Ax0jmujpU01wK7W4cLj1KIX;_ylv=3?qid=20110224130854AA9LGdy';
$input = 'yahoo';
$input = 'http://yahoo.com';
$input = 'I love http://yahoo.com';
output I love
Any domain with yahoo.com I want remove the output or return empty result. Maybe more than one domain. Let me know.
Try something like this:
$domains = array('yahoo.com', 'other-domain.org');
$domainsrgx = implode('|', array_map('preg_quote', $domains));
$filtered_userinput = preg_replace('#(^|\s+)(https?://)?([^/\s]*\.)?('.$domainsrgx.')(/[^\s]*)?(?=(\s|$))#is', '', $userinput);
This should remove everything from your example.
$data=str_replace('yahoo.com','',$data);
you can use an array to replace\remove multiple domains
$remove=array('yahoo.com','google.com');
$data=str_replace($remove,'',$data);
Ok. You can do
$rows = split("[\n|\r]", $input);
$output = "";
foreach($rows as $row){
if(strpos($row,"yahoo.com")){
continue;
}else{
$output = $row."\n";
}
}
I hope I'm not missing anything.
Related
How can I cut part of string before some word put on the end of the same string?
For example:
$data = '1.jpg,2.jpg,3.jpg,4.jpg,5.jpg,6.jpg,7.jpg,';
I want to put all before 4.jpg, at the end of string like this:
$data = '4.jpg,5.jpg,6.jpg,7.jpg,1.jpg,2.jpg,3.jpg,';
Here's something you can try.
<?php
$data = '1.jpg,2.jpg,3.jpg,4.jpg,5.jpg,6.jpg,7.jpg,';
$cut_name = '4.jpg'; //splice from #4
$data = explode(',', $data);
$cut_from = array_search($cut_name, $data);
array_pop($data);
$removed = array_splice($data, 0, $cut_from);
$data = implode(',', array_merge($data, $removed));
echo $data . ',';
You can use also some like this:
$data = '1.jpg,2.jpg,3.jpg,4.jpg,5.jpg,6.jpg,7.jpg';
function reorder_string($string,$separator){
$splited = explode($separator,$string);
//Explanations:
//$splited[0] = "1.jpg,2.jpg,3.jpg,"
//$splited[1] = "5.jpg,6.jpg,7.jpg"
//$separator = "4.jpg,"
//finally reorder
return $separator.$splited[1].",".$splited[0];
}
echo reorder_string($data,"4.jpg,");
This is too easy with this two-function, one-liner!
Code: (Demo)
$data = '1.jpg,2.jpg,3.jpg,4.jpg,5.jpg,6.jpg,7.jpg,';
$new_leader='4.jpg';
echo strstr($data,$new_leader),strstr($data,$new_leader,true);
Output:
4.jpg,5.jpg,6.jpg,7.jpg,1.jpg,2.jpg,3.jpg,
*note, can break if you change 2.jpg to 44.jpg due to unintended substring matching.
So I have attempted to get anything with a #texthere t change to a link so i can navigate them to the hash page of that specific hash.
With the code I have I just keep getting 'undefined' back. Can someone please take a look and point out where I am going wrong.
$json = array(
'userpost' => array()
);
$row = mysqli_fetch_assoc($check1);
$posts = array();
$posts['num'] = $num;
$posts['streamitem_id'] = $row['streamitem_id'];
$autoembed = new AutoEmbed();
$posts['streamitem_content'] = $autoembed->parse($row['streamitem_content']);
$regex = "/#(\w+)/";
$string=$row['streamitem_content'];
$string = preg_replace($regex, '$1', $string);
$posts['streamitem_content']=json_decode($string);
$posts['streamitem_creator'] = $row['streamitem_creator'];
$posts['streamitem_timestamp'] = $row['streamitem_timestamp'];
$posts['username'] = $row['username'];
$posts['id'] = $row['id'];
$posts['first'] = $row['first'];
$posts['middle'] = $row['middle'];
$posts['last'] = $row['last'];
$json['userpost'][] = $posts;
echo json_encode($json);
Okay this is what I've done to fix the issue. no decoding needed and that is what was causing the issue.
$regex = "/#(\w+)/";
$posts['streamitem_content'] = $row['streamitem_content'];
$posts['streamitem_content'] = preg_replace($regex, "
<a href='hash.php?tag=$1'>$1</a>", $posts['streamitem_content'] );
I'm a newbie in PHP ,andnow I'm struck on this problem . I have a string like this :
$string = "qwe,asd,zxc,rty,fgh,vbn";
Now I want when user click to "qwe" it will remove "qwe," in $string
Ex:$string = "asd,zxc,rty,fgh,vbn";
Or remove "fhg,"
Ex:$string = "asd,zxc,rty,vbn";
I try to user str_replace but it just remove the string and still have a comma before the string like this:
$string = ",asd,zxc,rty,fgh,vbn";
Anyone can help? Thanks for reading
Try this out:
$break=explode(",",$string);
$new_array=array();
foreach($break as $newData)
{
if($newData!='qwe')
{
$new_array[]=$newData;
}
}
$newWord=implode(",",$new_array);
echo $newWord;
In order to achieve your objective, array is your best friend.
$string = "qwe,asd,zxc,rty,fgh,vbn";
$ExplodedString = explode( "," , $string ); //Explode them separated by comma
$itemToRemove = "asd";
foreach($ExplodedString as $key => $value){ //loop along the array
if( $itemToRemove == $value ){ //check if item to be removed exists in the array
unset($ExplodedString[$key]); //unset or remove is found
}
}
$NewLook = array_values($ExplodedString); //Re-index the array key
print_r($NewLook); //print the array content
$NewLookCombined = implode( "," , $NewLook);
print_r($NewLookCombined); //print the array content after combined back
here the solution
$string = "qwe,asd,zxc,rty,fgh,vbn";
$clickword = "vbn";
$exp = explode(",", $string);
$imp = implode(" ", $exp);
if(stripos($imp, $clickword) !== false) {
$var = str_replace($clickword," ", $imp);
}
$str = preg_replace('/\s\s+/',' ', $var);
$newexp = explode(" ", trim($str));
$newimp = implode(",", $newexp);
echo $newimp;
You could try preg_replace http://uk3.php.net/manual/en/function.preg-replace.php if you have the module set up. It will allow you to optionally replace trailing or leading commas easily:
preg_replace("/,*$providedString,*/i", '', "qwe,asd,zxc,rty,fgh,vbn");
Now, I use strstr to get data from external JSON file. I'm not sure if it's the fastest way to do that what I want and I can't test because json_decode don't work in my code.
$before = '"THIS":"';
$after = '","date"';
$data = strstr(substr($url, strpos($url, $before) + strlen($before)), $after, true)
and with json_decode:
$address = file_get_contents('http://json.link/?something=Hello');
$data = json_decode($address);
echo $data->data->THIS;
Now, when I replace my first code with second I get:
Notice: Trying to get property of non-object
All my code:
$text = "a lot of text";
$text_split = array(0 => '');
$number = 0;
$words = explode(' ', $text);
foreach($words as $word)
{
if(strlen($text_split[$number]) + strlen($word) + 1 > 500)
{
++$number;
$text_split[$number] = '';
}
$text_split[$number] .= $word.' ';
}
foreach($text_split as $texts)
{
$text_encode = rawurlencode($texts);
$address = file_get_contents('http://json.link/?something='.$text_encode);
$data = json_decode($address);
echo $data->data->THIS;
}
What should in do in that case? Keep using strstr or replace all code to work with json_decode (maybe because execution time is faster?)? If the second option, how I can make json_decode work here? Thanks!
... and sorry for bad english.
LE:
If I replace $address = file_get_contents('http://json.link/?something='.$text_encode); with $address = file_get_contents('http://json.link/?something=Hello'); I get VALID result for "Hello" text but 10 times. I guess because it's in a foreach.
json_decode is the suggested method to work with JSON data. Here I think you are trying to access an invalid property in JSON object.
$data = json_decode($address);
echo $data->data->THIS;
I guess you need $data->date instead of $data-data?
you have to access the specific key value like this
$json = '{"success":true,"msg":"success","data":{"THIS":"thing I need","date":"24.03.2014","https":false}}';
$d=json_decode($json,true);
echo $d['data']['THIS'];
Basically what I want to do is display an email using javascript to bring the parts together and form a complete email address that cannot be visible by email harvesters.
I would like to take an email address eg info#thiscompany.com and break it to:
$variable1 = "info";
$variable2 = "thiscompany.com";
All this done in PHP.
Regards,
JB
list($variable1, $variable2) = explode('#','info#thiscompany.com');
$parts = explode("#", $email_address);
Assuming that $email_address = 'info#thiscompany.com' then $parts[0] == 'info' and $parts[1] == 'thiscompany.com'
You can use explode:
$email = 'info#thiscompany.com';
$arr = explode('#',$email);
$part1 = $arr[0]; // info
$part2 = $arr[1]; // thiscompany.com
$email = "info#thiscompany.com";
$parts = explode("#", $email);
Try this one before you roll your own (it does a lot more):
function hide_email($email)
{ $character_set = '+-.0123456789#ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz';
$key = str_shuffle($character_set); $cipher_text = ''; $id = 'e'.rand(1,999999999);
for ($i=0;$i<strlen($email);$i+=1) $cipher_text.= $key[strpos($character_set,$email[$i])];
$script = 'var a="'.$key.'";var b=a.split("").sort().join("");var c="'.$cipher_text.'";var d="";';
$script.= 'for(var e=0;e<c.length;e++)d+=b.charAt(a.indexOf(c.charAt(e)));';
$script.= 'document.getElementById("'.$id.'").innerHTML=""+d+""';
$script = "eval(\"".str_replace(array("\\",'"'),array("\\\\",'\"'), $script)."\")";
$script = '<script type="text/javascript">/*<![CDATA[*/'.$script.'/*]]>*/</script>';
return '<span id="'.$id.'">[javascript protected email address]</span>'.$script;
}
How about a function for parsing strings according to a given format: sscanf. For example:
sscanf('info#thiscompany.com', '%[^#]#%s', $variable1, $variable2);