이미지 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에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4796 | jQuery | 8년 전 | 1534 | ||
| 4795 | jQuery | 8년 전 | 1438 | ||
| 4794 | jQuery | 8년 전 | 1563 | ||
| 4793 | jQuery | 8년 전 | 1620 | ||
| 4792 | jQuery | 8년 전 | 1629 | ||
| 4791 | jQuery | 8년 전 | 1794 | ||
| 4790 | jQuery | 8년 전 | 1735 | ||
| 4789 | jQuery | 8년 전 | 1599 | ||
| 4788 | jQuery | 8년 전 | 1660 | ||
| 4787 | jQuery | 8년 전 | 1475 | ||
| 4786 | jQuery | 8년 전 | 1434 | ||
| 4785 | jQuery | 8년 전 | 1645 | ||
| 4784 | jQuery | 8년 전 | 1524 | ||
| 4783 | jQuery | 8년 전 | 1232 | ||
| 4782 | jQuery | 8년 전 | 1600 | ||
| 4781 | jQuery | 8년 전 | 1339 | ||
| 4780 | jQuery | 8년 전 | 1575 | ||
| 4779 | jQuery | 8년 전 | 1984 | ||
| 4778 | jQuery | 8년 전 | 1958 | ||
| 4777 | jQuery | 8년 전 | 1529 | ||
| 4776 | jQuery | 8년 전 | 1582 | ||
| 4775 | jQuery | 8년 전 | 1971 | ||
| 4774 | jQuery | 8년 전 | 1487 | ||
| 4773 | jQuery | 8년 전 | 1410 | ||
| 4772 | jQuery | 8년 전 | 2203 | ||
| 4771 | jQuery | 8년 전 | 3612 | ||
| 4770 | PHP | 8년 전 | 5579 | ||
| 4769 | 기타 | 8년 전 | 1964 | ||
| 4768 | 기타 | 8년 전 | 1781 | ||
| 4767 | 기타 | 8년 전 | 2067 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기