이미지 url주소값만 변경하기
[code]
$search = '<img border="0" src="http://aaa.com/0.jpg" width="100" height="100">
<br><img border="0" src="http://aaa.com/1.jpg" width="100" height="100">
<br><img border="0" src="http://aaa.com/2.jpg" width="100" height="100">';
$replace = '<img border="0" src="위에서 추출한 이미주소 첫번째" width="100" height="100">
<br>안녕하세요~
<br><br>날씨가 많이 춥네요
<br><img border="0" src="위에서 추출한 이미주소 두번째" width="100" height="100">
<br>감기조심하세요
<br><img border="0" src="위에서 추출한 이미주소 세번째" width="100" height="100">';
class UrlChange {
var $matches, $i = 0, $regex = '/(?<=src=")[^"]+/', $val = '';
function __construct($search, $replace) {
preg_match_all($this->regex, $search, $matches);
$this->matches = $matches[0];
$this->val = preg_replace_callback($this->regex, array($this, 'call'), $replace);
}
function call($m) { return $this->matches[$this->i++]; }
}
$a = new UrlChange($search, $replace);
echo $a->val;
[/code]
댓글 9개
new UrlChange($search, $replace);
요게 출력되는 코드 같은데 요거로 배열? 값 이거로 담으려면 어떻게 해야하는지요?
즉)
$aaa = "출력값";
이런식으로요
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<?
$list = array(
array('1','img/000.jpg', 'img/000on.jpg', '100', '100'),
array('2','img/001.jpg', 'img/001on.jpg', '100', '100'),
array('3','img/002.jpg', 'img/002on.jpg', '100', '100')
);
?>
<?php foreach($list as $v){ ?>
<img border="0" id="img_0<?=$v[0]?>" src="<?=$v[1]?>" width="<?=$v[3]?>" height="<?=$v[4]?>" data-img="<?=$v[2]?>">
<? } ?>
<script>
$(document).ready(function(){
$("img").click(function(){
var o = $(this).attr('id');
var c = $("#"+o).attr('src');
$("#"+o).attr('src', $(this).data("img"));
$("#"+o).data("img",c);
})
});
</script>
[/code]
저두 코딩 드럽게 못하는데 대충 만들어봤습니다^^
클릭시 data-img 변경으로 전 이미지로 돌아가도록 구성했습니다.
<?php
$list = array(
array('img/000.jpg', '100', '100'),
array('img/001.jpg', '100', '100'),
array('img/002.jpg', '100', '100')
);
foreach($list as $v)
printf('<img src="%s" width="%s" height="%s" >', $v[0], $v[1], $v[2]);
?>
<script>
var img = document.getElementsByTagName('img');
for (var i=0; i<img.length; i++) {
(function(k){
img[k].onclick = function() {
this.src = this.src.indexOf('_on') + 1 ? this.src.replace('_on', '') : this.src.replace('.', '_on.');
}
})(i);
}
</script>
[/code]
<?php
$list = array(
array('img/000.jpg', '100', '100'),
array('img/001.jpg', '100', '100'),
array('img/002.jpg', '100', '100')
);
foreach($list as $v)
printf('<img src="%s" width="%s" height="%s" >', $v[0], $v[1], $v[2]);
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$( 'img' ).on( 'click', function() {
this.src = this.src.indexOf('_on') + 1 ? this.src.replace('_on', '') : this.src.replace('.', '_on.');
});
</script>
[/code]
게시판 목록
개발자팁
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4856 | PHP | 7년 전 | 5735 | ||
| 4855 | jQuery | 7년 전 | 3239 | ||
| 4854 | jQuery | 7년 전 | 2229 | ||
| 4853 | jQuery | 7년 전 | 2832 | ||
| 4852 | MySQL | 7년 전 | 2995 | ||
| 4851 | jQuery | 7년 전 | 2342 | ||
| 4850 | jQuery | 7년 전 | 2572 | ||
| 4849 | jQuery | 7년 전 | 4310 | ||
| 4848 | PHP |
|
7년 전 | 4248 | |
| 4847 | jQuery | 7년 전 | 4021 | ||
| 4846 | jQuery | 7년 전 | 2640 | ||
| 4845 | jQuery | 7년 전 | 2154 | ||
| 4844 | jQuery | 8년 전 | 2304 | ||
| 4843 | jQuery | 8년 전 | 3057 | ||
| 4842 | jQuery | 8년 전 | 2900 | ||
| 4841 | jQuery | 8년 전 | 2231 | ||
| 4840 | jQuery | 8년 전 | 1570 | ||
| 4839 | jQuery | 8년 전 | 2469 | ||
| 4838 | jQuery |
이에스씨코리아
|
8년 전 | 2320 | |
| 4837 | jQuery | 8년 전 | 2167 | ||
| 4836 | jQuery | 8년 전 | 2094 | ||
| 4835 | jQuery | 8년 전 | 1828 | ||
| 4834 | jQuery | 8년 전 | 1868 | ||
| 4833 | jQuery | 8년 전 | 1995 | ||
| 4832 | jQuery | 8년 전 | 2185 | ||
| 4831 | jQuery | 8년 전 | 2366 | ||
| 4830 | jQuery | 8년 전 | 2632 | ||
| 4829 | jQuery | 8년 전 | 1804 | ||
| 4828 | jQuery | 8년 전 | 2749 | ||
| 4827 | jQuery | 8년 전 | 2288 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기